Django:扩展用户模型遇到的问题

3
我正在尝试向用户模型添加字段并将它们添加到管理页面。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'

尝试将用户个人资料字段添加到管理员页面:

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

为什么它无法识别那个特定的字段?

你会为现有用户创建UserProfile条目吗? - Sergey Lyapustin
我不太明白你的意思。我想要能够为现有用户和新用户创建新字段(UserProfile)。但是,我只有两个现有用户(管理员和测试用户)。 - babbaggeii
尝试交换内联内容。如果您不想要django.user,请注销它,注册Userprofile并为User添加一个内联。 - Chris
3个回答

2

编辑:查看完整的错误信息后,我可以看到错误不仅与扩展用户有关。当呈现用于将打印分配给正在编辑/添加的UserProfile的复选框和相应标签时,Django管理调用Print.__unicode__以呈现每个Print实例的标签,这反过来访问(/threedee/website/models.py第33行)Print的“printer”属性,该属性是指向User的外键。由于某种原因,其中一个Print具有无效的打印机值,该值不指向任何用户。

没有看到Print模型,真的无法确定发生了什么,请检查Print数据库表(应命名为website_print),并查找是否有任何异常情况(您是否使用PostgreSQL?)。如果您没有任何重要数据,则截断整个Print表应该可以解决问题。

这是我的旧答案,您仍然应该遵循它,但它与您正在经历的错误无关:

我只想评论其他人的答案,但似乎没有办法让我这样做。您需要结合Alexey Sidorov和Like it的答案:

首先,使用django shell为现有用户创建UserProfile实例-只需在shell中运行Like it的答案提供的命令(python manage.py shell)。

之后,您应该设置信号,该信号将根据alexey-sidorov的答案为每个新用户自动创建UserProfile。


您将能够在获得足够的“声望点数”后进行评论。 - S.L. Barth
谢谢。我刚刚做了这些事情,但仍然收到相同的错误信息:http://dpaste.com/815515/ - 不确定接下来该尝试什么。 - babbaggeii
好的,我可以看到问题可能出在哪里了,我已经编辑了我的答案。 - rychlis
太棒了!我已经截断了表website_print,因为那里只有一点数据。非常感谢。 - babbaggeii

1

我现在已经添加了,但是我仍然得到相同的错误:在渲染时捕获了DoesNotExist异常:没有匹配查询的用户。 - babbaggeii
你应该删除旧用户,添加另一个新用户。所以,请仔细阅读文档。 - Alexey Sidorov
我无法删除用户:http://dpaste.com/815484/,并且我无法添加新用户 - 我得到了与之前相同的错误。此外,我找不到文档中说明这一点的地方。 - babbaggeii

1

为现有用户添加用户配置文件条目:

from django.contrib.auth.models import User
from website.models import UserProfile
for user in User.objects.all():
    profile = UserProfile.objects.get_or_create(user = user)

同时为新用户创建个人资料。 使用 signals 是为了什么。

信号不是创建UserProfile的好方法。比如,如果您在contrib.auth中进行数据迁移或使用syncdb创建管理员用户,则信号将会中断,因为具有UserProfile的应用程序未同步。最好的方法是拥有一个自定义的auth后端,在登录时创建一个空的用户配置文件或类似的操作。甚至可以使用管理命令。但是,请避免使用信号。 - dekomote

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接