Fitering on manytomany field whose results are contained within another set...

63 views
Skip to first unread message

Jonty Needham

unread,
Dec 23, 2015, 7:32:05 AM12/23/15
to django...@googlegroups.com
I have a model of the form:

class Jobs(models.Model):
        names  = models.ManyToManyField(Workers)

class Workers(modesl.Model):
     is_a_problem = True


And I want the set of jobs that are done by workers who are a problem.

Something like

Jobs.objects.filter(names__is_contained_in=Workers.objects.filter(is_a_problem=True))

Obviously is_contained_in is not a valid lookup, so I would like to know how to do this please.

Thanks
Jonty

Vijay Khemlani

unread,
Dec 23, 2015, 7:45:34 AM12/23/15
to django...@googlegroups.com
You should be able to query it as a ForeignKey

Jobs.objects.filter(names__is_a_problem=True).distinct()

(distinct to prevent repeated jobs with more than one worker problem)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANOQRbz0TH53ix2hOCE7w8GkJMKPSB0utpzZgDzrAdXqn-Xzgw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jonty Needham

unread,
Dec 23, 2015, 11:51:14 AM12/23/15
to django...@googlegroups.com
Thanks :-) Should have guessed it was that easy!

Reply all
Reply to author
Forward
0 new messages