【发布时间】:2015-03-06 00:16:08
【问题描述】:
有类似的问题,但我相信我的问题不同。我对 Django 和 Python 很陌生,所以请原谅我的无知。
我有一个继承自 django.contrib.auth.models 用户类的自定义类 UserProfile。此 UserProfile 是基于 Tango with Django 中的练习,但是,我正在使用该示例来创建不同的项目/应用程序。
我在我的models.py中通过OneToOneField关系将UserProfile链接到标准User模型,如下所示:
class UserProfile(models.Model):
# Links UserProfile to a User model instance.
user = models.OneToOneField(User)
# The additional attribute I wish to include that isn't in User.
slug = models.SlugField(unique=True)
在我的 admin.py 文件中,我想要一个可以使用的 UserProfile 界面,并且我希望在我输入新的 UserProfile 时自动填充 slugfield。我希望它根据用户的用户名属性自动填充。但是,我似乎无法让它工作。这是我的 admin.py 代码:
class UserProfileAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("user.username",)}
当我尝试从命令行运行服务器时,出现以下错误:
ERRORS: <class 'climbcast.admin.UserProfileAdmin'>: (admin.E030) The
value of >'prepopula ted_fields["slug"][0]' refers to 'user.username',
which is not an attribute of >' climbcast.UserProfile'.
System check identified 1 issue (0 silenced).
它不允许我以这种方式访问 user.username 属性,即使我可以在 python shell 中以这种方式访问它。关于如何完成这项工作的任何想法?
【问题讨论】: