简体   繁体   English

无法在Django Admin中扩展用户模型?

[英]Can't extend user models in Django Admin?

The admin console doesn't show me the UserProfile in Django Admin. 管理控制台未在Django Admin中向我显示UserProfile There are no errors that show up. 没有显示任何错误。 I reloaded my server but it still doesn't show in the console. 我重新加载了服务器,但它仍未在控制台中显示。

from django.db import models
from django.contrib.auth.models import User


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=100, default='')
    city = models.CharField(max_length=100, default='')
    website = models.URLField(default='')
    phone = models.IntegerField(default=0)`

admin.py : admin.py

from django.contrib import admin
from account.models import UserProfile

admin.site.register(UserProfile)`

You need to create an admin model to your UserProfile like this example: 您需要为UserProfile创建一个管理模型,例如以下示例:

from django.contrib import admin
from account.models import UserProfile

@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'description', 'city', 'website', 'phone')
    # Other instance overrides come in this class and some new methods

As what Chiheb Nexus said , you need to create an admin model for your UserProfile . 正如Chiheb Nexus 所说的那样 ,您需要为UserProfile创建一个管理模型。 Additionally, you might need to subclass UserCreationForm , UserChangeForm , and UserAdmin in order to reflect the additional fields you have in your UserProfile model in Django Admin. 此外,您可能需要子类UserCreationFormUserChangeFormUserAdmin ,以反映Django Admin中UserProfile模型中的其他字段。

In admin.py , try doing something like this. admin.py ,尝试执行以下操作。

from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from django import forms

class UserProfileCreationForm(UserCreationForm):
    description = forms.CharField()
    city = forms.CharField()
    website = forms.URLField()
    phone = forms.IntegerField()

    def save(self, commit=True):
        user = super(UserProfileCreationForm, self).save(commit=False)
        user.description = self.cleaned_data['description']
        user.city = self.cleaned_data['city']
        user.website = self.cleaned_data['website']
        user.phone = self.cleaned_data['phone']

        if commit:
            user.save()

        return user

    class Meta:
        model = User  # Should this be UserProfile instead of User?
        fields = ('username', 'password1', 'password2', 'description', 'city', 'website', 'phone', 'first_name', 'last_name', 'is_active', 'is_staff', 'is_superuser')


class UserProfileChangeForm(UserChangeForm):
    description = forms.CharField()
    city = forms.CharField()
    website = forms.URLField()
    phone = forms.IntegerField()

    def save(self, commit=True):
        user = super(UserProfileChangeForm, self).save(commit=False)
        user.description = self.cleaned_data['description']
        user.city = self.cleaned_data['city']
        user.website = self.cleaned_data['website']
        user.phone = self.cleaned_data['phone']

        if commit:
            user.save()

        return user

    class Meta:
        model = User  # Again, should we use UserProfile instead?
        exclude = ('',)


class UserProfileAdmin(UserAdmin):
    form = UserProfileChangeForm
    add_form = UserProfileCreationForm
    list_filter = UserAdmin.list_filter + ('description', 'city', 'website', 'phone', )

    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (('Personal info'), {'fields': ('first_name', 'last_name', 'description', 'city', 'website', 'phone', )}),
        (('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser')}),
    )

    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 'description', 'city', 'website', 'phone', 'first_name', 'last_name', 'is_active', 'is_staff', 'is_superuser')}
        ),
    )

admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)

Also, I'm not sure but you might have to change your UserProfile model to something like this. 另外,我不确定,但是您可能必须将UserProfile模型更改为类似的形式。

from django.db import models
from django.contrib.auth.models import User


class UserProfile(models.Model):
    User.add_to_class('description', models.CharField(max_length=100, default=''))
    User.add_to_class('city', models.CharField(max_length=100, default=''))
    User.add_to_class('website', models.URLField(default=''))
    User.add_to_class('phone', models.IntegerField(default=0))

    user = models.OneToOneField(User)
    # Or user = models.ForeignKey(User, unique=False)
    # and maybe set unique to True.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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