Saturday, January 30, 2016

Re: Reverse Foriegnkey filtering

> On Jan 27, 2016, at 5:15 AM, Michal Petrucha <michal.petrucha@koniiiik.org> wrote:
>
> 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 also might need to follow up with .distinct() https://docs.djangoproject.com/en/1.8/ref/models/querysets/#django.db.models.query.QuerySet.distinct

Peter of the Norse
RahmCoff@Radio1190.org



--
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/C8D0965D-3FEB-44CB-95ED-B913C1497244%40Radio1190.org.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment