Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display child rows in Django admin interface

Is it possible to display a model's related child rows using the Django admin interface? An example model:

def Parent(models.Model):
    name = models.TextField()
    ....

def Child(models.Model):
    name = models.TextField()
    Parent = models.ForeignKey(Parent)
    ...

In the admin interface, when viewing a particular Parent object might display something like:

Name: Jack

Children: 

    Bob
    Jenny
    Sam
    ....

I understand that I can extend the admin views manually, just wondering if there's a bit of magic that I can add to my admin.py file instead.

like image 973
monofonik Avatar asked Jan 31 '12 12:01

monofonik


People also ask

What is Admin Modeladmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.

How use Django admin panel?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

Is Django's admin interface customizable if yes then how?

In this article, we will discuss how to enhance Django-admin Interface. Let us create an app called state which has one model with the same name(state). When we register app to admin.py it shows like. Now lets' customize django admin according to available options.


1 Answers

You could add the child objects as inlines.

class ChildInline(admin.TabularInline):
    model = Child

class ParentAdmin(admin.ModelAdmin):
    inlines = [
        ChildInline,
    ]
like image 93
Alasdair Avatar answered Oct 17 '22 03:10

Alasdair