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

时间:2018-01-14 04:56:49

标签: python django

管理控制台没有向我显示Django Admin中的UserProfile。没有出现任何错误。我重新加载了我的服务器,但它仍然没有在控制台中显示。

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

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

admin.site.register(UserProfile)`

2 个答案:

答案 0 :(得分:0)

您需要为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

答案 1 :(得分:0)

正如Chiheb Nexus said所述,您需要为UserProfile创建管理模型。此外,您可能需要创建UserCreationFormUserChangeFormUserAdmin的子类,以反映Django Admin中UserProfile模型中的其他字段。

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)

此外,我不确定,但您可能需要将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.