37

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 admin.py:

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.

Help?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Cody Hatch
  • 8,857
  • 6
  • 29
  • 36
  • 7
    I think it would be easiest to build your own view for this and plug it in Django admin and sparkle it with a dash of JavaScript to make things more dynamic. – Davor Lucic Sep 13 '10 at 13:35
  • Hm, I'm not really sure how that would work. Any example you can point to? :) – Cody Hatch Sep 19 '10 at 07:51

6 Answers6

21

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 http://code.djangoproject.com/ticket/9025 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.

Vasil
  • 36,468
  • 26
  • 90
  • 114
8

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

https://github.com/theatlantic/django-nested-admin

Jonathan
  • 8,453
  • 9
  • 51
  • 74
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
6

I have done this using https://github.com/theatlantic/django-nested-admin, for the following Data structure:

  • Contest
    • Judges
    • Contestants
      • Singers
      • Songs

My admin.pyfile:

from django.contrib import admin
import nested_admin

from .models import Contest, Contestant, Judge, Song, Singer    

class SongInline(nested_admin.NestedTabularInline):
    model = Song
    extra = 0

class SingerInline(nested_admin.NestedTabularInline):
    model = Singer
    extra = 0

class ContestantInline(nested_admin.NestedTabularInline):
    model = Contestant
    inlines = [SongInline, SingerInline]
    extra = 0

class JudgeInline(nested_admin.NestedTabularInline):
    model = Judge
    extra = 0

class ContestAdmin(nested_admin.NestedModelAdmin):
    model = Contest
    inlines = [ContestantInline, JudgeInline]
    extra = 0

admin.site.register(Contest, ContestAdmin)

https://github.com/theatlantic/django-nested-admin appears to be much more actively maintained than the other apps already mentioned (https://github.com/BertrandBordage/django-super-inlines and https://github.com/Soaa-/django-nested-inlines)

Li-Wen Yip
  • 325
  • 3
  • 9
  • What does `extra = 0` mean? – undefined Nov 05 '21 at 19:39
  • Ok this is a setting to show n empty rows so that new items can be added. With 0 empty rows you have to click on the 'Add another ... Relationship' button. Sorry for the email SPAM. – undefined Nov 05 '21 at 19:42
3

I have just ran into this issue as well... Seems this thread which contains the request for the nested inlines feature (https://code.djangoproject.com/ticket/9025#no2) has been updated with further information.

A custom made app called "django-super-inline" has been released. More details here: https://github.com/BertrandBordage/django-super-inlines

Installation and usage instructions below.

Hope this is useful for whomever comes across this.

enter image description here

Aivoric
  • 838
  • 2
  • 10
  • 24
2

I ran into a similar issue to this. My approach was to make an UpdateAdmin that held inlines for both Media and Post... it basically just makes it so you have a list of all of the media entries followed by all of the posts in an update.

class MediaInline(admin.StackedInline):
        model = Media

class PostInline(admin.StackedInline):
        model = Post

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

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

It isn't an ideal solution but it works for a quick and dirty work around.

carruthd
  • 341
  • 1
  • 3
  • 8
0

Use django-nested-admin which is the best package to do nested inlines.

First, install "django-nested-admin":

pip install django-nested-admin

Then, add "nested_admin" to "INSTALLED_APPS" in "settings.py":

# "settings.py"

INSTALLED_APPS = (
    # ...
    "nested_admin", # Here
)

Then, add "path('_nested_ad..." to "urlpatterns" in "urls.py":

# "urls.py"

from django.urls import include, path

urlpatterns = [
    # ...
    path('_nested_admin/', include('nested_admin.urls')), # Here
]

Finally, extend "NestedTabularInline" with "MediaInline()" and "PostInline()" classes and extend "NestedModelAdmin" with "UpdateAdmin()" class in "admin.py" as shown below:

# "admin.py"

from .models import Media, Post, Update
from nested_admin import NestedTabularInline, NestedModelAdmin

class MediaInline(NestedTabularInline):
    model = Media

class PostInline(NestedTabularInline):
    model = Post
    inlines = [MediaInline]

@admin.register(Update)
class UpdateAdmin(NestedModelAdmin):
    inlines = [PostInline]
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129