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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:32:53+00:00 2026-05-27T14:32:53+00:00

I have the models set up like this: class ParentModel(models.Model): some_col = models.IntegerField() some_other

  • 0

I have the models set up like this:

class ParentModel(models.Model):
    some_col = models.IntegerField()
    some_other = models.CharField()

class ChildModel(models.Model)
    parent = models.ForeignKey(ParentModel, related_name='children')

class ToyModel(models.Model)
    child_owner = models.ForeignKey(ChildModel, related_name='toys')

Now in my admin panel when I open the changelist for ParentModel I want a new field/column in the list_display with a link to open the changelist of the ChildModel but with an applied filter to show only the children from the selected parent. For now I realized it with this method, but I think there is a cleaner way to do it, I just don’t know how:

class ParentAdmin(admin.ModelAdmin)
    list_display = ('id', 'some_col', 'some_other', 'list_children')
    def list_children(self, obj):
        url = urlresolvers.reverse('admin:appname_childmodel_changelist')
        return '<a href="{0}?parent__id__exact={1}">List children</a>'.format(url, obj.id)
    list_children.allow_tags = True
    list_children.short_description = 'Children'        

admin.site.register(Parent, ParentAdmin)

So my question is, is it possible to achieve the same without this “link hacking”?
Also is it possible to indicate in a separate column in the ParentModel changelist, if any of its children has toys?

  • 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-27T14:32:54+00:00Added an answer on May 27, 2026 at 2:32 pm

    I think your approach to display the list_children column is correct. Don’t worry about the ‘link hacking’, it’s fine.

    To display a column for indicate whether any of the object’s children has toys, just define another method on the ParentAdmin class, and add it to list_display as before.

    class ParentAdmin(admin.ModelAdmin):
        list_display = ('id', 'some_col', 'some_other', 'list_children', 'children_has_toys')
        ...
        def children_has_toys(self, obj):
            """
            Returns 'yes' if any of the object's children has toys, otherwise 'no'
            """
            return ToyModel.objects.filter(child_owner__parent=obj).exists()
        children_has_toys.boolean = True
    

    Setting boolean=True means Django will render the ‘on’ or ‘off’ icons as it does for boolean fields. Note that this approach requires one query per parent (i.e. O(n)). You’ll have to test to see whether you get acceptable performance in production.

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

Sidebar

Related Questions

I have something like this: class Video(models.Model): user = models.ForeignKey(User, related_name='owner') ... and I'm
I have a couple of Django models set up like this: class Group(models.model): name
I have a simple model which looks like this: class Group(models.Model): name = models.CharField(max_length
I have a two models set up like this: class User < ActiveRecord::Base #
Suppose I have my models set up already. class books(models.Model): title = models.CharField... ISBN
I've got some models set up like this: class AppGroup(models.Model): users = models.ManyToManyField(User) class
I've got a model like this: class Talk(BaseModel): title = models.CharField(max_length=200) mp3 = models.FileField(upload_to
Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
I've got a model like this: class Thing(models.Model): property1 = models.IntegerField() property2 = models.IntegerField()
My django model looks like this: class Entity(models.Model): name = models.CharField(max_length=40) examples = models.ManyToManyField(Example,

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.