有几种型号:
class MyObject (models.Model):
name = models.CharField ()
class Day (models.Model):
day = models.IntegerField ()
class Month (models.Model):
month = models.IntegerField ()
myobj = models.ForeignKey (MyObject)
days = models.ManyToManyField (Day)在网站上使用表单的用户介绍一个月。我提出一个请求:
MyObject.objects.filter (month__month = <month, which is entered by the user with the form>
<day, which is entered by the user with the form> in month__days????
)如何查询用户输入的日期在模型月的m2m通信天数中是否存在?
发布于 2013-05-22 16:33:50
You can query M2M Fields just like you would FK relationships:
day = Day.objects.get(day=1)
Month.objects.filter(days=day)但是你需要在MyObject和Day的关系上做a reverse lookup
要引用“反向”关系,只需使用模型的小写名称即可。
day = Day.objects.get(day=5)
month = Month.objects.get(month=3)
MyObject.objects.filter(month__month=month, month__days=day)https://stackoverflow.com/questions/16683964
复制相似问题