I need separate views for add and change page. In add page I'd like to exclude some fields from inline formset. I've prepared two TabularInline classes, one of them contains property 'exclude'. I tried to use them as follows:
class BoxAdmin(admin.ModelAdmin):
def change_view(self, request, obj_id):
self.inlines=[ItemChangeInline,]
return super(BoxAdmin, self).change_view(self.request, obj_id)
def add_view(self, request):
self.inlines=[ItemAddInline,]
return super(BoxAdmin, self).add_view(self, request)
with no effect (no inline is shown at all).
It works with Django 1.5+ and seems fine & elegant:
hope it can be useful for anyone
I had a situation where I needed to show an Inline based on the admin site that you were on for a given story.
Expanding on alekwisnia's answer, I was able to get dynamic inlines working for Django 1.3 using the following code:
In highlights/admin.py
In story/admin.py
One thing to note is that I'm not merely manipulating inline classes(HighlightInline) but rather, I'm changing inline instances(HighlightInline(self.model, self.admin_site)). This is because django has already constructed a list of inline instances based on a list of inline classes during the initial construction of the admin class.
Here is the code that seems to be working:
However, this looks inelegant, cause this part:
is a copy-paste from init method of admin.ModelAdmin (so it is run twice).
Another solution to Django 1.3
Why in add_view you have
.add_view(self, request)
and in change view you have.change_view(self.request, ..)
? I believe, you don't need self in add_view, since you use super.