Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django hide fields only in CREATE object admin page

Tags:

django

As subject, i want to hide some fields ONLY when users enter the 'CREATE' admin page for specific model.

I know that change list_display can hide fields in admin page, but it's a global setting which will take affect not only in 'CREATE' admin page, but also in 'UPDATE' admin page.

like image 787
yunfeng.guo Avatar asked Nov 03 '16 06:11

yunfeng.guo


1 Answers

@admin.register(User)
class UserProfileAdmin(UserAdmin):
    def get_fields(self, request, obj=None):
        fields = super(UserProfileAdmin, self).get_fields(request, obj)
        for field in fields:
            if field == 'some_field_name' and obj is None:
                continue
            yield field
like image 146
Sergey Gornostaev Avatar answered Oct 13 '22 08:10

Sergey Gornostaev