Wednesday, January 27, 2016

Re: Reverse Foriegnkey filtering

On Wed, Jan 27, 2016 at 11:58:23AM +0000, Jonty Needham wrote:
> I've found a need to do this and I'm struggling. Some info seems to
> indicate that django doesn't support this. How is it meant to be done?
>
> Basically to be clear, I need to filter one queryset on a reverse
> foriegnkey relation to another set of objects defined by a particualr
> filter.
>
> model1(Models.model):
> stuff = charfield
>
> model2(Models.model):
> cow = Foreignkey(model1)
> country = charfield
>
>
> Problem: filter set of model1's by a particular country that's related to
> model1 through model2

If I read the problem correctly, the following should solve it::

model1.objects.filter(model2__country=my_country)

Cheers,

Michal

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/20160127121545.GB19381%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment