53

I've got a set of models that look like this:

class Page(models.Model):
    title = models.CharField(max_length=255)

class LinkSection(models.Model):
    page = models.ForeignKey(Page)
    title = models.CharField(max_length=255)

class Link(models.Model):
    linksection = models.ForeignKey(LinkSection)
    text = models.CharField(max_length=255)
    url = models.URLField()

and an admin.py that looks like this:

class LinkInline(admin.TabularInline):
    model = Link
class LinkSectionInline(admin.TabularInline):
    model = LinkSection
    inlines = [ LinkInline, ]
class PageAdmin(admin.ModelAdmin):
    inlines = [ LinkSectionInline, ]

My goal is to get an admin interface that lets me edit everything on one page. The end result of this model structure is that things are generated into a view+template that looks more or less like:

<h1>{{page.title}}</h1>
{% for ls in page.linksection_set.objects.all %}
<div>
    <h2>{{ls.title}}</h2>
    <ul>
         {% for l in ls.link_set.objects.all %}
        <li><a href="{{l.url}}">{{l.title}}</a></li>
         {% endfor %}
    </ul>
</div>
{% endfor %}

I know that the inline-in-an-inline trick fails in the Django admin, as I expected. Does anyone know of a way to allow this kind of three level model editing? Thanks in advance.

Serjik
  • 10,543
  • 8
  • 61
  • 70
The_OP
  • 667
  • 1
  • 8
  • 11

4 Answers4

22

You need to create a custom form and template for the LinkSectionInline.

Something like this should work for the form:

LinkFormset = forms.modelformset_factory(Link)
class LinkSectionForm(forms.ModelForm):
    def __init__(self, **kwargs):
        super(LinkSectionForm, self).__init__(**kwargs)
        self.link_formset = LinkFormset(instance=self.instance, 
                                        data=self.data or None,
                                        prefix=self.prefix)

    def is_valid(self):
        return (super(LinkSectionForm, self).is_valid() and 
                    self.link_formset.is_valid())

    def save(self, commit=True):
        # Supporting commit=False is another can of worms.  No use dealing
        # it before it's needed. (YAGNI)
        assert commit == True 
        res = super(LinkSectionForm, self).save(commit=commit)
        self.link_formset.save()
        return res

(That just came off the top of my head and isn't tested, but it should get you going in the right direction.)

Your template just needs to render the form and form.link_formset appropriately.

Krzysztof Szularz
  • 5,151
  • 24
  • 35
Matthew Marshall
  • 5,793
  • 2
  • 21
  • 14
  • 7
    Can we get sample template code for this answer? Is it advisable to fork Django's change_form.html for this? Also, in Django 1.2.3, the first line should be LinkFormSet = forms.inlineformset_factory(Link) – Bluu Dec 10 '10 at 19:59
  • 3
    Hi i am trying to get this to work but am getting errors with LinkFormset not wanting to take instance = self.instance, any reccomendations – Hugoagogo Feb 08 '12 at 00:05
  • 1
    Brilliant idea to nest the formset in the form. Will be testing that :-) – vdboor Jul 17 '12 at 07:58
  • `TypeError: __init__() got an unexpected keyword argument 'instance'` when using it, probably the same as @Hugoagogo – vmonteco Apr 14 '16 at 21:47
5

Django-nested-inlines is built for just this. Usage is simple.

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedStackedInline, NestedTabularInline
from models import A, B, C

class MyNestedInline(NestedTabularInline):
    model = C

class MyInline(NestedStackedInline):
    model = B
    inlines = [MyNestedInline,]

class MyAdmin(NestedModelAdmin):
    pass

admin.site.register(A, MyAdmin)
Ian Price
  • 7,416
  • 2
  • 23
  • 34
1

My recommendation would actually be to change your model. Why not have a ForeignKey in Link to LinkSection? Or, if it's not OneToMany, perhaps a ManyToMany field? The admin interface will generate that for free. Of course, I don't recommend this if links don't logically have anything to do with link sections, but maybe they do? If they don't, please explain what the intended organization is. (For example, is 3 links per section fixed or arbitrary?)

David Berger
  • 12,385
  • 6
  • 38
  • 51
  • Silly me, I ommited the ForeignKey field that I meant to be there :). The links do have to do with the link sections (intended as a header of sorts). The 3 links per section is arbitrary. I'll edit the OP to better reflect this. – The_OP Apr 08 '09 at 22:50
0

You can create a new class, similar to TabularInline or StackedInline, that is able to use inline fields itself.

Alternatively, you can create new admin templates, specifically for your model. But that of course overrules the nifty features of the admin interface.

pvoosten
  • 3,247
  • 27
  • 43