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 contai
Inspired by you guys answer, I was able to add more custom views to the admin.site.
Many times, just want add and change pages of different settings, not real extra views
# admin.py
class FooAdmin(admin.ModelAdmin):
....
def edit_tag(self, obj): # add a Link tag to change-list page
return mark_safe('Edit'.format(obj.get_absolute_url()))
edit_tag.short_description = u'Extra Action'
def change_view(self, request, object_id, form_url='', extra_context=None):
if request.GET.get('edit', False):
self.readonly_fields = (
'total_amount',
)
self.inlines = []
else:
self.readonly_fields = (
'name', 'client', 'constructor', 'total_amount'
)
self.inlines = [TransactionInline]
return super(ProjectAdmin, self).change_view(request, object_id)
def add_view(self, request, form_url='', extra_context=None):
self.readonly_fields = (
'total_amount',
)
self.inlines = []
return super(ProjectAdmin, self).add_view(request)
After this I'll have three views:
add view - without inline formset, no need to add related objects.
change view 1 - with inline formset, only for adding inline data(related objects), the the object's field is readonly.
change view 2 - without inline formset, only for changing the object.
Really simple, and we can do more, thanks everyone.