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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:41:05+00:00 2026-06-05T18:41:05+00:00

So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):

  • 0

So, say I have models like this:

class Foo(Model):
    name = CharField(max_length=200)
    def latest_comment(self):
        try:
            object = self.comment_set.latest()
            if object:
                return object.when_posted.date()
        except:
            return ""

class Comment(Model):
   when_posted = DateTimeField()
   text = TextField()

Then this is the modelAdmin:

class FooAdmin(ModelAdmin):
    list_display = ['name', 'latest_comment']
    ordering = ['latest_comment']

admin.site.register(Foo, FooAdmin)

It throws an error when I go to the admin site saying that ‘latest_comment’ was not found in app.Foo. Having it in list_display works fine. So, my question is: Is there a way to order models in list_display by methods of the model? And if so, how?

  • 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-05T18:41:06+00:00Added an answer on June 5, 2026 at 6:41 pm

    I haven’t tested the code, but just as an idea to try to implement this with overriding the queryset of the ModelAdmin with a annotate function to sort over a field from related field:

    class FooAdmin(admin.ModelAdmin):
        def queryset(self, request):
            qs = super(FooAdmin, self).queryset(request)
    
            return qs.distinct() \
            .annotate(date_of_last_comment=Max('comment__date')) \
            .order_by('-date_of_last_comment')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a Django class something like this: class Person(models.Model): name = models.CharField(max_length=50)
Let's say I have two models looking like this: class ProductType(models.Model): product_type = models.CharField(max_length=100)
Say I have models: class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print
Let's say I have these models: class A(models.Model): name = models.CharField(max_length=32) b = models.ForeignKey(B,
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
Say I have the good old person/group/membership relationship class Person(models.Model): name = models.CharField(max_length=128) def
Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many
Say I have a model like this: class Book(models.Model): user = models.ForeignKey(User) book_isbn =
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar

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.