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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:20:04+00:00 2026-05-28T03:20:04+00:00

I have an object: class Image(models.Model): width = models.IntegerField(…) height = models.IntegerField(…) class Foo(models.Model):

  • 0

I have an object:

class Image(models.Model):
    width = models.IntegerField(...)
    height = models.IntegerField(...)

class Foo(models.Model):
    title = ...
    image = models.ForeignKey(Image ...)

and I want to try and retrieve all Foo objects that have a related image with a similar aspect ratio. In a perfect world, something like:

ratio = width/height
min = ratio - 0.25
max = ratio + 0.25

similar = ImageMeta.objects \
              .annotate(ratio=Div('image__width', 'image__height')\
              .filter(ratio__gte=min).filter(ratio__lte=max)

but there is no such divison aggregation function. This answer suggests using extra, i.e.

.extra({ 'select' : 'width/height' )

but because the width and height are in a different table, I’m not sure how to span the relationship (i.e. write the SQL) to do this. Could anyone offer some help?

  • 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-05-28T03:20:05+00:00Added an answer on May 28, 2026 at 3:20 am

    Consider using F() objects instead of aggregation.

    This query multiplies by the ratio to do straight comparisons on a single field:

    Foo.objects.filter(image__width__gte=F('image__height') * min_ratio, 
                       image__width__lte=F('image__height') * max_ratio)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery) class Image(models.Model): Image = models.ImageField(upload_to=gallery)
I have these Models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery) class Image(models.Model): Image = models.ImageField(upload_to=gallery)
I have an object of class F. I want to output the contents of
it's possible to have one entity object (class, e.g. User) for more entity models
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have two models, Room and Image . Image is a generic model that
I have a model and form like so: class Image(BaseModel): original = db.BlobProperty() class
I have a class which serves as a model for an image: class Image
take a look at my models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery,editable=True,blank=True,null=True) class Image(models.Model): Image
I have this object: class a { public string Application; public DateTime From, To;

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.