Filter over multivalued relationships

116 views
Skip to first unread message

Savannah

unread,
Nov 12, 2011, 6:56:55 AM11/12/11
to django-filter
Hi guys,

I found your django-filter app and am trying to get it working right
now. There is however one problem I do not know how to solve. It's
almost exactly the same thing as is described in django documentation:

https://docs.djangoproject.com/en/1.2/topics/db/queries/#spanning-multi-valued-relationships

I want to make a query where I select all Blogs that has an entry with
both "Lennon" in headline and was published in 2008, eg.:

Blog.objects.filter(entry__headline__contains='Lennon',
entry__pub_date__year=2008)

Not to select Blogs that has an entry with "Lennon" in headline and
another entry with pub in 2008:

Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008)

However, if I set up Filter such that there are two fields:

class BlogFilter(django_filters.FilterSet):
entry__headline = django_filters.CharFilter()
entry__pub_date = django_filters.CharFilter()

class Meta:
model = Blog
fields = ['entry__headline', 'entry__pub_date', ]

django-filter will generete the latter:

Blog.objects.filter(entry__headline__exact='Lennon').filter(entry__pub_date__exact=2008)

Is there a way to combine both filters into a single filter field?

Steve

unread,
Nov 12, 2011, 6:59:45 AM11/12/11
to django...@googlegroups.com
Filtering across joins is a bug without a simple solution. I use https://github.com/subsume/django-filter-actually-maintained

Savannah

unread,
Nov 19, 2011, 6:09:17 AM11/19/11
to django-filter
Hi Steve,

I've tried the version you provided. However, it does not work either.
Is there a workaround? For example to write a custom method on
BlogFilter that will add filter for entru__headline and entry__pub_dat
fields?

On 12 lis, 12:59, Steve <subs...@gmail.com> wrote:
> Filtering across joins is a bug without a simple solution. I usehttps://github.com/subsume/django-filter-actually-maintained


>
>
>
>
>
>
>
> On Sat, Nov 12, 2011 at 6:56 AM, Savannah <skalicka.ond...@gmail.com> wrote:
> > Hi guys,
>
> > I found your django-filter app and am trying to get it working right
> > now. There is however one problem I do not know how to solve. It's
> > almost exactly the same thing as is described in django documentation:
>

> >https://docs.djangoproject.com/en/1.2/topics/db/queries/#spanning-mul...

Steve

unread,
Nov 19, 2011, 10:07:19 AM11/19/11
to django...@googlegroups.com
Workaround would be create your own filter that does both lookups.

Something.objects.filter(foo__bar__blah=x).filter(boo__bar__bluck=y) <-- creates two joins

Something.objects.filter(foo__bar__blah=x, boo__bar__bluck=y) <-- creates one joins
Reply all
Reply to author
Forward
0 new messages