简体   繁体   中英

Django admin How to solve the problem that all parts are not visible?

I created a system with Django. I create an abstract user model with some custom fields. My form is working but in the admin page I cannot see my custom models.

models.py

class UserProfile(AbstractUser):
    ranks = (
        ...
        ('cfo', 'Chief Financial Officier'),
    )
    comp_name = models.CharField(max_length=200, default="Choose")
    user_id = models.UUIDField(default=uuid.uuid4(), editable=False, unique=True)
    username = models.CharField(max_length=500, unique=True)
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    password = models.CharField(max_length=50)
    email = models.EmailField(max_length=254)
    rank = models.CharField(max_length=200, choices=ranks)

    def __str__(self):
        return self.username

class CompanyProfile(models.Model):
...

admin.py

    admin.site.register(CompanyProfile)


class CustomUser(UserAdmin):
    add_form = SignUpForm
    model = UserProfile
    form = SignUpChangeForm
    list_display = ('username', 'first_name', 'email', 'last_name', 'rank', 'comp_name')
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
       
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2', 'rank', 'comp_name')}
         ),
    )
    search_fields = 'email'


admin.site.register(UserProfile, UserAdmin)

管理视图

As you can see in admin view there is no ranks and company name field. How can I solve it?

You should add them to fieldsets aswell, from the docs

Note : If you are using a custom ModelAdmin which is a subclass of django.contrib.auth.admin.UserAdmin, then you need to add your custom fields to fieldsets (for fields to be used in editing users) and to add_fieldsets (for fields to be used when creating a user). For example:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM