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

The Archive Base Latest Questions

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

I have two tables like so: class Collection(models.Model): name = models.CharField() class Image(models.Model): name

  • 0

I have two tables like so:

class Collection(models.Model):
    name = models.CharField()

class Image(models.Model):
    name = models.CharField()
    image = models.ImageField()
    collection = models.ForeignKey(Collection)

I’d like to retrieve the first image out of every collection. I have attempted:

image_list = Image.objects.order_by('collection.id').distinct('collection.id')

but it didn’t work out the way I expected 🙁

Any ideas?
Thanks.

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

    Don’t use dots to separate fields that span relations in Django; the double-underscore convention is used instead — it means “follow this relation to get to this field”

    this is more correct:

    image_list = Image.objects.order_by('collection__id').distinct('collection__id')
    

    However, it probably doesn’t do what you want.

    The concept of “first” doesn’t always apply in relational databases the way you seem to be using it. For all of the records in the image table with the same collection id, there is no record which is ‘first’ or ‘last’ — they’re all just records. You could put another field on that table to define a specific order, or you could order by id, or alphabetically by name, but none of those will happen by default.

    What will probably work best for you is to get the list of collections with one query, and then get a single item per collection, in separate queries:

    collection_ids = Image.objects.values_list('collection', flat=True).distinct()
    image_list = [
        Image.objects.filter(collection__id=c)[0] for c in collection_ids
    ]
    

    If you want to apply an order to the Images, to define which is ‘first’, then modify it like this:

    collection_ids = Image.objects.values_list('collection', flat=True).distinct()
    image_list = [
        Image.objects.filter(collection__id=c).order_by('-id')[0] for c in collection_ids
    ]
    

    You could also write raw SQL — MySQL aggregation has the interesting property that fields which are not aggregated over can still appear in the final output, and essentially take a random value from the set of matching records. Something like this might work:

    Image.objects.raw("SELECT image.* FROM app_image GROUP BY collection_id")
    

    This query should get you one image from each collection, but you will have no control over which one is returned.

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

Sidebar

Related Questions

Say that I have two tables like those: Employers (id, name, .... , deptId).
I have two tables, name and address. I would like to list the last_name
I have two tables: Companies: (id, name) Workers: (id, name, country) I would like
I have two tables like the ones below. I need to find what exchangeRate
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
Let's say I have two tables that look like this: TH TH TH TH
I have two tables which looks something like this Table Queue int ID; string
I have two tables A and B. I would like to delete all the
I have two different tables, lead and click. I would like to query MySQL
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,

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.