Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:44:58+00:00 2026-06-11T01:44:58+00:00

I’m using django-filter app. There is however one problem I do not know how

  • 0

I’m using django-filter app. 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 (possibly the same) that was published 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 (nevermind __contains x __exact, just an example):

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:44:59+00:00Added an answer on June 11, 2026 at 1:44 am

    Well, I came with a solution. It is not possible to do using the regular django-filters, so I extended it a bit. Could’ve been improved, this is quick-n-dirty solution.

    1st added a custom “grouped” field to django_filters.Filter and a filter_grouped method (almost copy of filter method)

    class Filter(object):
    
        def __init__(self, name=None, label=None, widget=None, action=None,
            lookup_type='exact', required=False, grouped=False, **kwargs):
            (...)
            self.grouped = grouped
    
        def filter_grouped(self, qs, value):
            if isinstance(value, (list, tuple)):
                lookup = str(value[1])
                if not lookup:
                    lookup = 'exact' # we fallback to exact if no choice for lookup is provided
                value = value[0]
            else:
                lookup = self.lookup_type
            if value:
                return {'%s__%s' % (self.name, lookup): value}
            return {}
    

    the only difference is that instead of creating a filter on query set, it returns a dictionary.

    2nd updated BaseFilterSet qs method/property:

    class BaseFilterSet(object):
        (...)
        @property
        def qs(self):
            if not hasattr(self, '_qs'):
                qs = self.queryset.all()
                grouped_dict = {}
                for name, filter_ in self.filters.iteritems():
                    try:
                        if self.is_bound:
                            data = self.form[name].data
                        else:
                            data = self.form.initial.get(name, self.form[name].field.initial)
                        val = self.form.fields[name].clean(data)
                        if filter_.grouped:
                            grouped_dict.update(filter_.filter_grouped(qs, val))
                        else:
                            qs = filter_.filter(qs, val)
                    except forms.ValidationError:
                        pass
    
                if grouped_dict:
                    qs = qs.filter(**grouped_dict)
    
            (...)
        return self._qs
    

    The trick is to store all “grouped” filters in a dictionary and then use them all as a single filter.

    The filter will look something like this then:

    class BlogFilter(django_filters.FilterSet):
        entry__headline = django_filters.CharFilter(grouped=True)
        entry__pub_date = django_filters.CharFilter(grouped=True)
    
        class Meta:
            model = Blog
            fields = ['entry__headline', 'entry__pub_date', ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.