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 9007895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:48:42+00:00 2026-06-16T01:48:42+00:00

What is the neutral element for django.db.models.Q objects for | operation? I’d like to

  • 0

What is the neutral element for django.db.models.Q objects for | operation? I’d like to generate a filter with a function:

MyModel.objects.filter(myfunc(args)) where myfunc should give something like: "Q(foo) | Q(bar) | ... | False"

but I don’t know what is False for Q objects. Similarly, I would need the neutral element for & operation (True)…

Here is an example of such a function:

# Models
class MyModel(models.Model):
    myfield1 = models.CharField(max_length=30)
    myfield2 = models.CharField(max_length=30)


# Views
class MyView(views.View):
    model = MyModel

    def get_queryset(self):
        def myfunc(query_object_list, param):
            myfuncr = lambda l: ((myfuncr(l[1:]) | Q(**{ param: l[0] })) if l else Q(False)) # "Q(False)" would be the neutral element of operation "|" for Q objects
            return myfuncr(query_object_list)

        myq = Q(True) # "Q(True)" would be the neutral element of operation "&" for Q objects
        for param in self.request.GET:
            myq &= myfunc(self.request.GET.getlist(param, None), param)

        return MyModel.objects.filter(myq)


# Template
<a href='{% url myview %}?myfield1__iexact={{ myvar1 }}&myfield2__iexact={{ myvar2 }}'>foobar</a>
  • 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-16T01:48:43+00:00Added an answer on June 16, 2026 at 1:48 am

    I finally found the solution, which was in fact pretty simple… the “neutral element” for Q objects, which is the same for operations “|” and “&” is: “Q()”

    I now have a dynamic filter which can match everything I want in my templates…

    # Models
    class MyModel(models.Model):
        myfield1 = models.CharField(max_length=30)
        myfield2 = models.CharField(max_length=30)
        myinteger3 = models.IntegerField(blank=True, null=True)
    
    
    
    # Views
    class MyView(views.View):
        model = MyModel
    
        def get_queryset(self):
            def myfunc(query_object_list, param):
                myfuncr = lambda l: ((myfuncr(l[1:]) | Q(**{ param: l[0] })) if l else Q()) # Q() is the neutral element of operation "|" for Q objects
                return myfuncr(query_object_list)
    
            myq = Q() # Q() is the neutral element of operation "&" for Q objects
            for param in self.request.GET:
                myq &= myfunc(self.request.GET.getlist(param, None), param)
    
            return MyModel.objects.filter(myq)
    
    
    # Template
    <a href='{% url myview %}?myfield1__iexact={{ myvar1 }}&myfield2__iexact={{ myvar2 }}&myinteger3__gte={{ myvar3 }}'>foobar</a>
    
    <a href='{% url myview %}?myinteger3__gte={{ myvar4 }}'>{{ myvar4 }}</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if there is any way to generate culture neutral CSV file or
Is it possible to make a filter, for example a PropertyFilter that is neutral
I have a jQuery function that displays a hidden content on each element in
I need to convert some 2-letter (neutral) identifiers (like en for English) so something
OK, so the Core Image filter Temperature and Tint has two inputs, neutral and
jQuery provides a browser neutral library for accessing and manipulating the DOM. This is
Money, Staff, Skill and preference to open source or commercial is neutral . Let's
I have a simple function about neural network. This function gets a matrix, loads
I want to generate simple drawings in my asp.net website based on some data
JPA promises to be vendor neutral for persistence and database. But I already know

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.