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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:51:36+00:00 2026-05-11T01:51:36+00:00

I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage

  • 0

I have a following model:

class Car(models.Model):     make = models.CharField(max_length=40)     mileage_limit = models.IntegerField()     mileage = models.IntegerField() 

I want to select all cars where mileage is less than mileage_limit, so in SQL it would be something like:

select * from car where mileage < mileage_limit; 

Using Q object in Django, I know I can compare columns with any value/object, e.g. if I wanted to get cars that have mileage say less than 100,000 it would be something like:

cars = Car.objects.filter(Q(mileage__lt=100000)) 

Instead of a fixed value I would like to use the column name (in my case it is mileage_limit). So I would like to be able to do something like:

cars = Car.objects.filter(Q(mileage__lt=mileage_limit)) 

However this results in an error, since it is expecting a value/object, not a column name. Is there a way to compare two columns using Q object? I feel like it would be a very commonly used feature and there should be an easy way to do this, however couldn’t find anything about it in the documentation.

Note: this is a simplified example, for which the use of Q object might seem to be unnecessary. However the real model has many more columns, and the real query is more complex, that’s why I am using Q. Here in this question I just wanted to figure out specifically how to compare columns using Q.

EDIT

Apparently after release of Django 1.1 it would be possible to do the following:

cars = Car.objects.filter(mileage__lt=F('mileage_limit')) 

Still not sure if F is supposed to work together with Q like this:

cars = Car.objects.filter(Q(mileage__lt=F('mileage_limit'))) 
  • 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. 2026-05-11T01:51:37+00:00Added an answer on May 11, 2026 at 1:51 am

    You can’t do this right now without custom SQL. The django devs are working on an F() function that would make it possible: #7210 – F() syntax, design feedback required.

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

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer To answer the primary question on how to avoid the… May 11, 2026 at 2:15 pm
  • added an answer From iphonedevsdk.com: The accelerometers used in the first and second… May 11, 2026 at 2:15 pm
  • added an answer The viEmu page itself has a pretty good article that… May 11, 2026 at 2:15 pm

Related Questions

I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I am novice in sharepoint programming. I have a following code: SPWorkflowTask task =
I have a the following scenarios. I am trying to calculate throughput of the
I have a web application that comprises the following: A web project (with a
For Example: Let's say that I want to return a view that displays a
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
Let's say I have the following model: class Contest: title = models.CharField( max_length =
I have a sequence of migrations in a rails app which includes the following

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.