different fields for add and change pages in admin
django, django-admin, python
Solution
First have a look at source of ModelAdmin class' `get_form` and `get_formsets` methods located in `django.contrib.admin.options.py`. You can override those methods and use kwargs to get the behavior you want. For example:
class SoftwareVersionAdmin(ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
# Proper kwargs are form, fields, exclude, formfield_callback
if obj: # obj is not None, so this is a change page
kwargs['exclude'] = ['foo', 'bar',]
else: # obj is None, so this is an add page
kwargs['fields'] = ['foo',]
return super(SoftwareVersionAdmin, self).get_form(request, obj, **kwargs)
Problem
I have a django app with the following class in my admin.py: ``` class SoftwareVersionAdmin(ModelAdmin): fields = ("product", "version_number", "description", "media", "relative_url", "current_version") list_display = ["product", "version_number", "size", "current_version", "number_of_clients", "percent_of_clients"] list_display_links = ("version_number",) list_filter = ['product',] ``` I want to have these fileds for add page but different fields for change page. How can I do that?