修改默认用户模型后 Django 管理面板不起作用
我试图扩展用户配置文件。我创建了一些解决方案,但最推荐的是创建包含原始 django.contrib.auth.models.User 类的外键的新用户类。我用 this 所以我在 models.py
:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
website_url = models.URLField(verify_exists=False)
和我的 admin.py
from django.contrib import admin
from someapp.models import *
from django.contrib.auth.admin import UserAdmin
# Define an inline admin descriptor for UserProfile model
class UserProfileInline(admin.TabularInline):
model = UserProfile
fk_name = 'user'
max_num = 1
# Define a new UserAdmin class
class MyUserAdmin(UserAdmin):
inlines = [UserProfileInline, ]
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
现在当我尝试创建/在管理面板中编辑用户我有一个错误: “‘字段列表’中存在未知列‘content_userprofile.id’”
,其中 content 是我的应用程序名称。
我试图将行 AUTH_PROFILE_MODULE = 'content.UserProfile'
添加到我的 settings.py
但没有效果。
如何告诉面板管理员如何正确显示用户表单中的字段?
I was trying to extend user profile. I founded a few solutions, but the most recommended was to create new user class containing foreign key to original django.contrib.auth.models.User class. I did it with this so i have in models.py
:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
website_url = models.URLField(verify_exists=False)
and in my admin.py
from django.contrib import admin
from someapp.models import *
from django.contrib.auth.admin import UserAdmin
# Define an inline admin descriptor for UserProfile model
class UserProfileInline(admin.TabularInline):
model = UserProfile
fk_name = 'user'
max_num = 1
# Define a new UserAdmin class
class MyUserAdmin(UserAdmin):
inlines = [UserProfileInline, ]
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
And now when I'm trying to create/edit user in admin panel i have an error:"Unknown column 'content_userprofile.id' in 'field list'"
where content is my appname.
I was trying to add line AUTH_PROFILE_MODULE = 'content.UserProfile'
to my settings.py
but with no effect.
How to tell panel admin to know how to correctly display fields in user form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番努力,我找到了可行的解决方案:
AUTH_PROFILE_MODULE = 'content.UserProfile'
python manage.pysyncdb
After some effort I found working solution:
AUTH_PROFILE_MODULE = 'content.UserProfile'
is requiredpython manage.py syncdb