简体   繁体   中英

Can't extend user models in Django Admin?

The admin console doesn't show me the UserProfile in Django Admin. 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 :

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:

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 . 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.

In admin.py , try doing something like this.

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.

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.

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