python - django How to query ManyToManyField contains a certain id
某草草
某草草 2017-07-05 10:34:50
0
1
851

A task can be assigned to multiple people. Now we want to query all the tasks assigned (assigns) to user_id = 1. How to write?

class Task(models.Model):
    assigns = models.ManyToManyField(User, blank=True, verbose_name='指派给')
    # User 是系统自带的 User 模型

How to query?

Task.object.filter(?如何查询?)
某草草
某草草

reply all(1)
phpcn_u1582

Documentation:
https://docs.djangoproject.co...

Task.object.filter(user_id__exact=1)

But it should also work:

Task.object.get(user_id=1)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template