Opened 13 years ago
Closed 13 years ago
#17380 closed Uncategorized (wontfix)
Default value of ModelAdmin.inlines can be altered.
Reported by: | Matthew Schinckel | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Because ModelAdmin.inlines is defined as an empty list, if you have not altered this in a ModelAdmin subclass, and then add to it, you actually add that inline to every ModelAdmin subclass that does not override inlines.
class MyModelAdmin(admin.ModelAdmin):
pass
if 'other_app' in settings.INSTALLED_APPS:
MyModelAdmin.inlines += [OtherInline]
This bug rears its head if you try to add two (or more) inlines to UserAdmin.
I do have what seems like a fix: in ModelAdmin.init(), we can do:
self.inlines = self.inlines or []
This will set self.inlines to a new empty list if the original value was falsy.
Change History (2)
follow-up: 2 comment:1 by , 13 years ago
comment:2 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I would say it's the expected behavior.
I agree. Mutating mutable objects mutates them. Class attributes that aren't overridden by a subclass are still the superclass attribute. This is just how Python works; I can't see what other behavior would be expected, or what reasonable code would "fix" this. It would have to be a class property, I think, which is quite unusual and just not worth it.
Your fix won't work:
If you want to achieve this behavior you should re-define inlines as an empty list when subclassing ModelAdmin:
I would say it's the expected behavior.