Opened 6 years ago

Closed 6 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:

Description

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)
    self.inline_instances.append(inline_instance)

Change History (3)

comment:1 Changed 6 years ago by dc

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

You can use:

admin.site.unregister(FlatPageAdmin)
FlatPageAdmin.inlines += [FlatPageAttachment]
admin.site.register(FlatPage, FlatPageAdmin)

comment:2 Changed 6 years ago by zbyte64

Only works if inlines is a list, no?

admin.site.unregister(FlatPageAdmin)
FlatPageAdmin.inlines = list(FlatPageAdmin.inlines)
FlatPageAdmin.inlines += [FlatPageAttachment]
admin.site.register(FlatPage, FlatPageAdmin)

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

comment:3 Changed 6 years ago by Alex

  • Resolution set to invalid
  • Status changed from new to closed

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