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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:50:40+00:00 2026-05-15T20:50:40+00:00

I have the following models: class Order_type(models.Model): description = models.CharField() class Order(models.Model): type= models.ForeignKey(Order_type)

  • 0

I have the following models:

class Order_type(models.Model):
    description = models.CharField()

class Order(models.Model):
    type= models.ForeignKey(Order_type)
    order_date = models.DateField(default=datetime.date.today)
    status = models.CharField()
    processed_time= models.TimeField()

I want a list of the order types that have orders that meet this criteria: (order_date <= today AND processed_time is empty AND status is not blank)

I tried:

qs = Order_type.objects.filter(order__order_date__lte=datetime.date.today(),\
     order__processed_time__isnull=True).exclude(order__status='')

This works for the original list of orders:

orders_qs = Order.objects.filter(order_date__lte=datetime.date.today(), processed_time__isnull=True)
orders_qs = orders_qs.exclude(status='')

But qs isn’t the right queryset. I think its actually returning a more narrowed filter (since no records are present) but I’m not sure what. According to this (django reference), because I’m referencing a related model I think the exclude works on the original queryset (not the one from the filter), but I don’t get exactly how.

OK, I just thought of this, which I think works, but feels sloppy (Is there a better way?):

qs = Order_type.objects.filter(order__id__in=[o.id for o in orders_qs])
  • 1 1 Answer
  • 2 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-15T20:50:40+00:00Added an answer on May 15, 2026 at 8:50 pm

    What’s happening is that the exclude() query is messing things up for you. Basically, it’s excluding any Order_type that has at least one Order without a status, which is almost certainly not what you want to happen.

    The simplest solution in your case is to use order__status__gt='' in you filter() arguments. However, you will also need to append distinct() to the end of your query, because otherwise you’d get a QuerySet with multiple instances of the same Order_type if it has more than one Order that matches the query. This should work:

    qs = Order_type.objects.filter(
        order__order_date__lte=datetime.date.today(),
        order__processed_time__isnull=True,
        order__status__gt='').distinct()
    

    On a side note, in the qs query you gave at the end of the question, you don’t have to say order__id__in=[o.id for o in orders_qs], you can simply use order__in=orders_qs (you still also need the distinct()). So this will also work:

    qs = Order_type.objects.filter(order__in=Order.objects.filter(
        order_date__lte=datetime.date.today(),
        processed_time__isnull=True).exclude(status='')).distinct()
    

    Addendum (edit):

    Here’s the actual SQL that Django issues for the above querysets:

    SELECT DISTINCT "testapp_order_type"."id", "testapp_order_type"."description"
        FROM "testapp_order_type"
        LEFT OUTER JOIN "testapp_order"
        ON ("testapp_order_type"."id" = "testapp_order"."type_id")
            WHERE ("testapp_order"."order_date" <= E'2010-07-18'
            AND "testapp_order"."processed_time" IS NULL
            AND "testapp_order"."status" > E'' );
    
    SELECT DISTINCT "testapp_order_type"."id", "testapp_order_type"."description"
        FROM "testapp_order_type"
        INNER JOIN "testapp_order"
        ON ("testapp_order_type"."id" = "testapp_order"."type_id")
            WHERE "testapp_order"."id" IN
                (SELECT U0."id" FROM "testapp_order" U0
                    WHERE (U0."order_date" <= E'2010-07-18'
                    AND U0."processed_time" IS NULL
                    AND NOT (U0."status" = E'' )));
    

    EXPLAIN reveals that the second query is ever so slightly more expensive (cost of 28.99 versus 28.64 with a very small dataset).

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

Sidebar

Related Questions

I have the following model class Command(models.Model): server = models.ForeignKey(Server) user_login = models.CharField(max_length=100) user_run
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) primary_phone = models.OneToOneField('Phone', related_name='is_primary',
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
I have the following two models class Author(Model): name = CharField() class Publication(Model): title
I have a following (simplified) class in models.py : class Order( models.Model ) :
I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
i have the following: class Tag( models.Model ): name = models.CharField( max_length=64 ) class

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.