Opened 10 years ago

Closed 9 years ago

#10939 closed (invalid)

Allow for dynamic registration of inlines into an admin model

Reported by: zbyte64 Owned by: nobody
Component: contrib.admin Version: 1.0
Severity: Keywords: inlines
Cc: zbyte64@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


It used to be (django 0.96) you could arbitrarily add inlines into an admin model:

from django.db import models
from django.contrib.models import FlatPage

class FlatPageAttachment(models.Model):
    flatpage = models.ForeignKey(FlatPage, edit_inline=True)
    attachment = models.FileField(upload_to="flatpages")

Now this is not possible. You could try to import the FlatPage admin and attach an admin inline, but this will not work. The reason for this is that when the Admin model is registered into the admin site, it goes through and populates inline_instances. It is true you could unregister and re-register, but what if another app also wanted to add an inline? If that was the case then only one would get its inline registered.

Perhaps a better alternative is to add a method to ModelAdmin called "register_inline". It would behave something like the following:

def register_inline(self, inline_class):
    inline_instance = inline_class(self.model, self.admin_site)

Change History (3)

comment:1 Changed 10 years ago by dc

You can use:
FlatPageAdmin.inlines += [FlatPageAttachment], FlatPageAdmin)

comment:2 Changed 10 years ago by zbyte64

Only works if inlines is a list, no?
FlatPageAdmin.inlines = list(FlatPageAdmin.inlines)
FlatPageAdmin.inlines += [FlatPageAttachment], FlatPageAdmin)

I suppose I didn't want 4 lines of code for something that used to be so easy :/

comment:3 Changed 9 years ago by Alex Gaynor

Resolution: invalid
Status: newclosed

Closing as invalid since it's still possiblem either my monkey patch or by unregister, alter, reregister.

Note: See TracTickets for help on using tickets.
Back to Top