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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:57:49+00:00 2026-06-10T06:57:49+00:00

I have 2 models: class Call(models.Model): date_time_created = models.DateTimeField(auto_now_add = True) comment = models.CharField(max_length

  • 0

I have 2 models:

class Call(models.Model):
    date_time_created = models.DateTimeField(auto_now_add = True)
    comment = models.CharField(max_length = 300, blank= True, null=True)
    bought_record = models.ForeignKey('BoughtRecords')

class BoughtRecords(models.Model):
    date_time_bought = models.DateTimeField(auto_now_add= True)
    bought_packet = models.ForeignKey('BoughtPackets')

I wrote 2 queries:

  1. Q1 = Q(call__result = 1)
    company_for_call = BoughtRecords.objects.exclude(Q1)

  2. company_for_call = BoughtRecords.objects.exclude(call__result = 1)

    I expected the same behaviour from this queries. They generate slightly different sql-queries( I am not good at raw sql=) ).

I suppose thats why Call not always exists for BoughtRecord or may be Q-objects not properly work with backward foreign keys. Explain me please this behaviours, because I often compile complex queries from Q-objects and sometimes get unexpected result. I want to know about situations in which at first sight result must be simular, but in reality diffent.

  • 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-10T06:57:50+00:00Added an answer on June 10, 2026 at 6:57 am

    I can see that in case of using Q there is an INNER_JOIN with a WHERE NOT while calling the exclude directly results in the query being a nested select. Semantically they are equivalent. When the INNER JOIN happens the NULL references on the foreign_key are excluded. In the case of nested query target_call.bought_record_id = NULL is explicitly specified. So there should not be any difference in the results.

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

Sidebar

Related Questions

I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
Following is the models.py: class UserProfile(models.Model): user = models.OneToOneField(User) belongs_to_user_category = models.ForeignKey(UserCustomCategory, null=True, blank=True)
I have models like this: class Vendor(models.Model): title = models.CharField() class Product(models.Model): ... vendor
I have a model that looks like this: class Item(models.Model): ... publish_date = models.DateTimeField(default=datetime.datetime.now)
I have these models: class Model1(models.Model): ... class Model2(models.Model): m1 = models.OneToOneField(Model1, related_name='m2') ...
Let's see if I can explain myself, I have this models: class BillHeader(models.Model): number
I have a model whereby I have an abstract class (lets call it Vehicle
I have three models: class User include Mongoid::Document field :name, :type => String has_many

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.