对于django管理员,如何添加一个字段到用户模型,并在管理员中可以编辑? [英] For the django admin, how do I add a field to the User model and have it editable in the admin?
问题描述
我正在尝试更好地了解django管理员,同时我想再向当前用户管理员添加一个字段。在models.py我已经完成了
User.add_to_class('new_field',models.BooleanField(default = False))
并且在admin.py中我已经得到以下内容(其中fieldets基本上只是从django / contrib / auth / admin.py)
class AdjUserAdmin(UserAdmin):
list_display = UserAdmin.list_display +('new_field' ,)
list_filter = UserAdmin.list_filter +('new_field',)
fieldsets = UserAdmin.fieldsets
fieldsets [1] [1] ['fields'] =(' first_name','last_name','email','new_field')
问题是,当我这样做我得到错误:
AdjUserAdmin.fieldsets [4] [1] ['fields']'我们已经看过UserChangeForm,但它看起来已经正确地拉动了。以用户为模型。我不知道为什么表单中缺少
new_field
。
谢谢
关于这是臭味代码
我知道这是一个臭猴子修补方式去这样做,但子类化问题主要针对这些的原因..如果我能够按照上述方式工作,我会很开心..也许会有臭味。
关于以推荐的方式
我知道建立用户个人资料的方式,只是在特殊情况下,我看不到优点在创建一个全新的表,并且当我想要存储的所有数据库都有额外的调用是额外的一些信息,如 is_private
或其中一些。如果我正在存储更多的信息,那么我同意设置一个用户配置文件是比较好的。
它插入?问题 - 您是否在数据库中的users表中手动添加了new_field?当然,Syncdb不会照顾到这一点。
之后,我会尝试将字段添加到现有的UserAdmin上,而不是从头开始重新构建:
from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display + =('new_field',)#不要忘记逗号
UserAdmin .list_filter + =('new_field',)
UserAdmin.fieldsets + =('new_field',)
I'm trying to understand the django admin better and at the same time, I'm trying to add one more field to the current user admin. In models.py I've done
User.add_to_class('new_field', models.BooleanField(default=False))
and in admin.py I've got the following (with fieldsets basically just copied from django/contrib/auth/admin.py)
class AdjUserAdmin(UserAdmin):
list_display = UserAdmin.list_display + ('new_field',)
list_filter = UserAdmin.list_filter + ('new_field',)
fieldsets = UserAdmin.fieldsets
fieldsets[1][1]['fields'] = ('first_name','last_name','email','new_field')
The problem is, when I do this I get the error:
AdjUserAdmin.fieldsets[4][1]['fields']' refers to field 'new_field' that is missing from the form.
I've looked at UserChangeForm, but it looks like it's already correctly pulling in User as the model. I'm not sure as to why new_field
is missing from the form.
Thanks
In regards to this being smelly code
I know this is a smelly monkey patching way to go about doing this, but subclassing gives me issues mainly for these reasons.. if I could get it to work the way stated above, I'd be happy.. and maybe smelly.
In regards to the recommended way
I'm aware of the recommended way of creating a user profile, just that in particular situations, I don't see the merit in creating an entire new table and having an additional call to the database when all I want to store is an extra bit of information such as is_private
or some such. If I'm storing lots more info, then I agree, setting up a user profile is preferable.
First the "is it plugged in?" question -- Have you manually added new_field to the users table in the database? Syncdb wouldn't have taken care of that, of course.
After that, I would try appending the fields onto the existing UserAdmin rather than rebuilding it from scratch:
from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display += ('new_field',) # don't forget the commas
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets += ('new_field',)
这篇关于对于django管理员,如何添加一个字段到用户模型,并在管理员中可以编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!