different fields for add and change pages in admin

前端 未结 9 744
日久生厌
日久生厌 2020-12-05 00:37

I have a django app with the following class in my admin.py:

class SoftwareVersionAdmin(ModelAdmin):
    fields = (\"product\", \"version_number\", \"descrip         


        
9条回答
  •  醉梦人生
    2020-12-05 00:58

    I don't think it's a good idea to override fields or exclude or form, because they are config attributes, so they would not initialize for every request.
    I think the accepted answer by shanyu is a good solution.

    Or we can use the method from UserAdmin:

    def get_fieldsets(self, request, obj=None):                                  
        if not obj:                                                                                                 
            return self.add_fieldsets                                            
        return super(UserAdmin, self).get_fieldsets(request, obj)  
    

    Remember to assign the add_fieldsets yourself. Unfortunately it doesn't fit my use case.

    For Django 1.7. I don't know how they are implemented in other versions.

提交回复
热议问题