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

  • Home
  • SEARCH
  • 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 7026549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:07:56+00:00 2026-05-28T00:07:56+00:00

How do I create a query which filters a model like this? My model

  • 0

How do I create a query which filters a model like this?

My model is:

class test(models.Model):
    name = models.CharField(max_length=50)
    status = models.CharField(max_length=50)
    other = models.CharField(max_length=50)

I would like such that instead of OR I filter the query by AND. I have tried changing the | to & but it does not work. To make it more complex, if I have filters on the template as a form such that:

Filter By Name: DropDownList of Names
Filter By Status: DropDownList of Statuses
Filter By Other: DropDownList of Others

If I choose name1, status1 and other1 from the drop down boxes it should display the results of:

results = test.objects.filter(name=name1,status=status1,other=other1)

Here is my current query in views.py:

def search(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
            Q(name__icontains=query) |
            Q(status__icontains=query) |
            Q(other__icontains=query)
        )
        results = test.objects.filter(qset).distinct()
    else:
        results = []
    return render_to_response("test/search.html", {
        "results": results,
        "query": query
    })

This currently only searches name, status or other. If I change the | to & it searches for all of them but nothing shows when I display results because the query processing from the form drop down boxes are not linked. How to link them? How to also have an ALL option for the drop downs for the specific field?

I hope I have made this clear. Thank you.

  • 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-05-28T00:07:57+00:00Added an answer on May 28, 2026 at 12:07 am

    I recommend you take a look django-filters application. It can automatize all routine with form dropdowns and making querysets by the search/filter query:

    Django-filter is a reusable Django application for allowing users to
    filter queryset dynamically. It requires Python 2.4 or higher. For
    usage and installation instructions consult the docs directory.

    Django-filter can be used for generating interfaces similar to the
    Django admin’s list_filter interface. It has an API very similar
    to Django’s ModelForms. For example if you had a Product model
    you could have a filterset for it with the code::

    import django_filters
    
    class ProductFilterSet(django_filters.FilterSet):
        class Meta:
            model = Product
            fields = ['name', 'price', 'manufacturer']
    

    And then in your view you could do::

    def product_list(request):
        filterset = ProductFilterSet(request.GET or None)
        return render_to_response('product/product_list.html',
            {'filterset': filterset})
    

    See the docs directory for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a compiled query which uses reusable where predicates. An
I usually use a construct like this to apply filters to columns: CREATE TABLE
I want to create a query which has more than 3-4 Expression.Or ? But
I would like to create a query that returns rows in the following format:
i wonder what this query means: $blogPost = Doctrine_Query::create()->from('BlogPost p')->where('p.slug = ?', 'doctrine-released')->execute(); could
I've this query, which works fine except it takes a long while (7 seconds,
I have an access query which creates some output, 3 columns named e.g. A
I created a simple .Net web service which runs a very simple query (which
How can I create a query for a full outer join across a M2M
Hi I need to create a query in MSAccess 2003 through code (a.k.a. VB)

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.