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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:37:32+00:00 2026-05-25T15:37:32+00:00

# admin.py class CustomerAdmin(admin.ModelAdmin): list_display = (‘foo’, ‘number_of_orders’) # models.py class Order(models.Model): bar =

  • 0
# admin.py
class CustomerAdmin(admin.ModelAdmin):  
    list_display = ('foo', 'number_of_orders')


# models.py
class Order(models.Model):
    bar = models.CharField[...]
    customer = models.ForeignKey(Customer)

class Customer(models.Model):
    foo = models.CharField[...]

    def number_of_orders(self):
        return u'%s' % Order.objects.filter(customer=self).count()  

How could I sort Customers, depending on number_of_orders they have?

admin_order_field property can’t be used here, as it requires a database field to sort on. Is it possible at all, as Django relies on the underlying DB to perform sorting? Creating an aggregate field to contain the number of orders seems like an overkill here.

The fun thing: if you change url by hand in the browser to sort on this column – it works as expected!

  • 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-25T15:37:32+00:00Added an answer on May 25, 2026 at 3:37 pm

    I loved Greg’s solution to this problem, but I’d like to point that you can do the same thing directly in the admin:

    from django.db import models
    
    class CustomerAdmin(admin.ModelAdmin):
        list_display = ('number_of_orders',)
    
        def get_queryset(self, request):
        # def queryset(self, request): # For Django <1.6
            qs = super(CustomerAdmin, self).get_queryset(request)
            # qs = super(CustomerAdmin, self).queryset(request) # For Django <1.6
            qs = qs.annotate(models.Count('order'))
            return qs
    
        def number_of_orders(self, obj):
            return obj.order__count
        number_of_orders.admin_order_field = 'order__count'
    

    This way you only annotate inside the admin interface. Not with every query that you do.

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

Sidebar

Related Questions

I have this sample model working with the admin class Author(models.Model): name = models.CharField(_('Text
I have a model such as the following: class Item(models.Model): name = models.CharField(max_length=150) created
My models: class Contact(models.Model): first_name = models.CharField(_(First name), max_length=30, ) last_name = models.CharField(_(Last name),
In my admin.py I have: class SayfaAdmin(admin.ModelAdmin): def queryset(self, request): qs = super(SayfaAdmin, self).queryset(request)
Greetings, I have these 2 models: from django.db import models class Office(models.Model): name =
Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
I'm a blind student currently in a system admin/shell programming class. Although ssh works
I added some models in my models.py and I want to add an admin
Hey, I'm fairly new to Django, and I'm looking to edit admin class variables
In the admin interface and newforms there is the brilliant helper of being able

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.