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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:44:49+00:00 2026-06-14T03:44:49+00:00

in my database there is a table that holds some user information. The table

  • 0

in my database there is a table that holds some user information. The table is called UserBalance and fields include user, credits_in and credits_out and remark (among others)

I am trying to sum the credits_in for a certain user but I get different sums for different cases. Take a look at this:

>>> cg = UserBalance.objects.filter(user=ranked_user).filter(remark__icontains='credits g').aggregate(sum_in=Sum('credits_in'))
>>> cg
{'sum_in': 35.85}
>>> cg = UserBalance.objects.filter(user=ranked_user).filter(Q(remark='credits gained') or Q(remark='credits given')).aggregate(sum_in=Sum('credits_in'))
>>> cg
{'sum_in': 26.16}
>>> cg = UserBalance.objects.filter(user=ranked_user).filter(Q(remark='credits given') or Q(remark='credits gained')).aggregate(sum_in=Sum('credits_in'))
>>> cg
{'sum_in': 9.69}

in the first case I used i_cointains, in the second and third cases I use a Q() but with its terms switched.

Can someone explain what is the difference between the 2nd and the 3rd case?

  • 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-14T03:44:50+00:00Added an answer on June 14, 2026 at 3:44 am

    You should use (Q(remark='credits gained')|Q(remark='credits given')), not(Q(remark='credits gained') or Q(remark='credits given'))

    It’s 2 completely different operators:
    | is bitwise OR, but it’s overriden for Q():

    def __or__(self, other):
        return self._combine(other, self.OR)
    

    while or is logical (or rather ‘coalescing’) operator. It means that Q(remark='credits gained') or Q(remark='credits given') will return first not-None object, that’s why in your second case it will result in Q(remark='credits gained'), and in the third one – in Q(remark='credits given')

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

Sidebar

Related Questions

As usual, some background information first: Database A (Access database) - Holds a table
In a MySQL (5.1) database table there is data that represents: how long a
Is there any application that will read a MySQL database table and generate a
I have a database in which there is a user table & how many
I have a table that holds phone calls, with the following fields: ID STARTTIME
Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.
Is there a free utility that will fill up your database tables with test
Is there a SQL command that will list all the tables in a database
In our database there is a table which is created with ANSI_NULLS OFF .
There is a table employee in the database abc_db at abc@localhost(server) and there is

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.