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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:24:46+00:00 2026-06-19T00:24:46+00:00

First example: # ANDing Q objects q_object = Q() q_object.add(Q(), Q.AND) # ORing Q

  • 0

First example:

# ANDing Q objects
q_object = Q()
q_object.add(Q(), Q.AND)

# ORing Q objects
q_object = Q()
q_object.add(Q(), Q.OR)

Second example:

>>> import operator
# create a list of Q objects
>>> mylist = [Q(question__contains='dinner'), Q(question__contains='meal')]
# OR
>>> Poll.objects.filter(reduce(operator.or_, mylist))
[<Poll: what shall I make for dinner>, <Poll: what is your favourite meal?>]
# AND
>>> Poll.objects.filter(reduce(operator.and_, mylist))
[]

This technique might be very useful, for building queries for pages with conditional-filters for example, like on eBay.

But this things, as I know – not documented, so what best practices are exist for this matter, which will not be dropped from support, and will not confuse people who will read my code?

ps
And also – is it good solution to use “&” operator with Q() objects? In Django-docs I found nothing about it!

  • 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-19T00:24:47+00:00Added an answer on June 19, 2026 at 12:24 am

    Check the doc
    It’s fine to use & or operator.and_ to represent 'AND', or shorter:

    >>> mylist = [Q(question__contains='dinner'), Q(question__contains='meal')]
    # AND
    >>> Poll.objects.filter(reduce(operator.and_, mylist))
    # could be 
    >>> Poll.objects.filter(*mylist)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following this example (First comment), How to create a custom ListView with
When attempting to run the first example in the boost::mpi tutorial , I was
Assume methods M1 and M2 have strongly related responsibilities First example: If • M1
I have a simple marker annotation for methods (similar to the first example in
If I have three files index.php file.php and fns.php First example (it works): index.php
Could someone explain this (strange) behavior? Why is the length in the first example
I am learning ADO.NET entity framework. This is my first example. The datagrid seems
This is just a simple question. Either way works. I prefer my first example,
Using the Programming F# book, there's an example first given as serial: let result1
Can somebody explain to me why classes are not first class objects in Java?

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.