0

有型号:

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????
)

如何在模型 Month 的 m2m 通信天数中查看用户输入的日期是(存在)的查询?

4

1 回答 1

0

可以像查询 FK 关系一样查询 M2M 字段

day = Day.objects.get(day=1)
Month.objects.filter(days=day)

但是您需要对and之间的关系进行反向查找MyObjectDay

要引用“反向”关系,只需使用模型的小写名称。

day = Day.objects.get(day=5)
month = Month.objects.get(month=3)

MyObject.objects.filter(month__month=month, month__days=day)
于 2013-05-22T08:33:49.630 回答