我有一个模特:
class Tasks(models.Model):
name = models.CharField(max_length = 50, null = True, blank = True)
assigned_to = models.ManyToManyField(User, null = True, blank = True)
我必须执行查询
tasks_for_myuser = Tasks.objects.filter(assigend_to__contains = myuser)
但这会引发错误。
django.core.exceptions.FieldError: Related Field got invalid lookup: contains
请帮助!
答案 0 :(得分:1)
如果您尝试过滤将assigned_to
字段设置为myuser
的任务,则可以像这样简单地查询。
tasks_for_myuser = Tasks.objects.filter(assigend_to = myuser)
您实际上并不需要contains
,因为它是一个多对多字段。