我正在尝试将字段添加到用户模型并将它们添加到管理页面。这里的 django 文档中有一个推荐的方法:
https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
因此,我为我的新模型创建了一个 OneToOne 字段:
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
designs = models.ManyToManyField('Design', blank=True)
prints = models.ManyToManyField('Print', blank=True)
rating = models.IntegerField(null=True, blank=True)
reliability = models.IntegerField(null=True, blank=True)
av_lead_time = models.IntegerField(null=True, blank=True)
在 settings.py 中添加了 AUTH_PROFILE_MODULE:
AUTH_PROFILE_MODULE = 'website.UserProfile'
尝试将 UserProfile 字段添加到管理页面:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from website.models import UserProfile
from django.contrib.auth.models import User
# Define an inline admin descriptor for UserProfile model
# which acts a bit like a singleton
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'profile'
# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (UserProfileInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
现在,当我尝试通过管理菜单访问注册用户时,我得到:
Caught DoesNotExist while rendering: User matching query does not exist.
In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
19 {{ field.field }}
当我尝试通过管理菜单添加新用户时,我得到:
Caught DoesNotExist while rendering: User matching query does not exist.
In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
19 {{ field.field }}
为什么它不识别那个特定的领域?