136
Have you tried something like this:
module.workflow_set.filter(trigger_roles__in=[self.role], allowed=True)
or just if self.role.id
is not a list of pks:
module.workflow_set.filter(trigger_roles__id__exact=self.role.id, allowed=True)
27
The simplest approach to achieve this would be checking for equalty over the whole instance (instead of the id) in the ManyToManyField
. That looks if the instance is inside the many to many relationship. Example:
module.workflow_set.filter(trigger_roles=self.role, allowed=True)
- [Django]-How to access a dictionary element in a Django template?
- [Django]-Get object by field other than primary key
- [Django]-Django get the static files URL in view
10
I know this is an old question, but it looks like the OP never quite got the answer he was looking for. If you have two sets of ManyToManyFields you want to compare, the trick is to use the __in
operator, not contains
. So for example if you have an “Event” model with a ManyToMany to “Group” on field eventgroups
, and your User model (obviously) attaches to Group, you can query like this:
Event.objects.filter(eventgroups__in=u.groups.all())
- [Django]-Pass request context to serializer from Viewset in Django Rest Framework
- [Django]-Favorite Django Tips & Features?
- [Django]-Django – SQL bulk get_or_create possible?
7
singularity is almost right with the first example. You just need to make sure it’s a list. The second example, checking the trigger_roles__id__exact
is a better solution though.
module.workflow_set.filter(trigger_roles__in=[self.role.id],allowed=True)
- [Django]-Django 2.0 – Not a valid view function or pattern name (Customizing Auth views)
- [Django]-Django: how save bytes object to models.FileField?
- [Django]-Is it secure to store passwords as environment variables (rather than as plain text) in config files?