1👍
✅
ModelAdmin
has a method called get_inline_instances()
that can be used to achieve this effect.
class TestAdmin(admin.ModelAdmin):
def get_inline_instances(self, request, obj=None):
if obj.id > 3:
self.inlines = [TestInfoInline, ]
else:
self.inlines = []
return super(TestAdmin, self).get_inline_instances(request, obj)
Second example – the effect remains exactly the same:
class TestAdmin(admin.ModelAdmin):
inlines_foo = []
inlines_bar = [TestInfoInline, ]
def get_inline_instances(self, request, obj=None):
self.inlines = self.inlines_foo if obj.id <= 3 else self.inlines_bar
return super(TestAdmin, self).get_inline_instances(request, obj)
0👍
Well I think your problem is related to the issue discussed here Misleading documentation in ModelAdmin.get_inline_instances. So essentially your code should be changed to something like…
# Assuming you have a TestInfoInline inline that inherits from a BaseModelAdmin defined
# similarly to below.
class TestInfoInline(admin.StackedInline):
# Relevant stuff here…
…
class TestAdmin(admin.ModelAdmin):
def get_inline_instances(self, request, obj=None):
inlines = []
if obj.id > 3:
for inline_class in self.inlines:
# Instanciate inline object
inline = inline_class(self.model, self.admin_site)
inlines.append(inline)
return inlines
Hope this helps someone…
👤ooo
- [Answer]-Django foreignkey – format output value in selectlist
- [Answer]-Attach mixin to model when it is created based on a field?
- [Answer]-Django redirect still shows Form View on redirect
Source:stackexchange.com