Nested inlines in the Django admin?


Alright, I have a fairly simple design.

class Update(models.Model):
    pub_date = models.DateField()
    title = models.CharField(max_length=512)

class Post(models.Model):
    update = models.ForeignKey(Update)
    body = models.TextField()
    order = models.PositiveIntegerField(blank=True)

class Media(models.Model):
    post = models.ForeignKey(Post)
    thumb = models.ImageField(upload_to='frontpage')
    fullImagePath = models.ImageField(upload_to='frontpage')

Is there an easy-ish way to allow a user to create an update all on one page?

What I want is for a user to be able to go to the admin interface, add a new Update, and then while editing an Update add one or more Posts, with each Post having one or more Media items. In addition, I want the user to be able to reorder Posts within an update.

My current attempt has the following in

class MediaInline(admin.StackedInline):
    model = Media

class PostAdmin(admin.ModelAdmin):
    inlines = [MediaInline,]

This let's the user add a new Post item, select the relevant Update, add the Media items to it, and hit save - which is fine. But there's no way to see all the Posts that belong to a given Update in a single place, which in turn means you can't roderder Posts within an update. It's really quite confusing for the end user.


12/19/2015 9:42:41 AM

Accepted Answer

As of now there is no "built-in" way to have nested inlines (inline inside inline) in django.contrib.admin. Pulling something like this off is possible by having your own ModelAdmin and InlineModelAdmin subclasses that would enable this kind of functionality. See the patches on this ticket for ideas on how to implement this. You'd also need to provide your own templates that would have nested iteration over both the top level inline and it's child inline.

9/19/2010 10:37:38 AM

There is now this egg available, which is a collation of the relevant patches mentioned in the other answer:

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow