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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:39:46+00:00 2026-06-18T04:39:46+00:00

# models.py class Gallery(models.Model): images = models.ManyToManyField(Image, null=True, blank=True) class Image(models.Model): image = models.ImageField()

  • 0
# models.py
class Gallery(models.Model):
    images = models.ManyToManyField(Image, null=True, blank=True)

class Image(models.Model):
    image = models.ImageField()


# views.py
class GalleryIndex(ListView):
    model = Gallery

I need to get thumbnail for every gallery, which is it’s very first/last/whatever image.
How can I LIMIT image by 1 for any gallery as a custom attribute (to not override Gallery.images) without calling second SQL query?

  • 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-18T04:39:47+00:00Added an answer on June 18, 2026 at 4:39 am

    I should’ve read docs better. Standard QuerySet API can’t handle such cases efficiently (annotate() generates GROUP BY clause for each of parent fields which is slow) so I come with extra() method and raw subquery.

    class GalleryIndex(ListView):
        queryset = Gallery.objects.extra(select={'thumb':
            """
            SELECT "image"
            FROM "app_image", "app_gallery_images"
            WHERE (
                "app_gallery"."id" = "app_gallery_images"."gallery_id"
                AND "app_gallery_images"."image_id" = "app_image"."id"
            )
            AND "app_image"."image" IS NOT NULL
            LIMIT 1
            """
        })
    

    This queryset do exactly what I wanted, since SorlImageField (and ImageField) needs only filename to represent thumbnail in templates.

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

Sidebar

Related Questions

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 these models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery,blank=True,null=True) class News(Gallery): Name=models.CharField(max_length=100) #some other
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
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'm using django.contrib.admin in one of my apps. my models: class Gallery(models.Model): location =
My Gallery model is related to my image model, but when I retrive images
Models: class Device extends Backbone.Model url: '/device' initialize: -> console.log Device model created... @set
Example models: class Parent(models.Model): name = models.CharField() def __unicode__(self): return self.name class Child(models.Model): parent
Given the following models: class Post(models.Model): title = models.CharField(max_length=200) html = models.TextField() class PostTag(models.Model):

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.