对于django管理员,如何在User模型中添加字段并使其在管理员中可编辑?

时间:2022-09-12 08:48:03

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

我试图更好地理解django管理员,同时,我正在尝试向当前用户管理员添加一个字段。在models.py我做过

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)

在admin.py我有以下内容(字段集基本上只是从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.

AdjUserAdmin.fieldsets [4] [1] ['fields']'指的是表单中缺少的字段'new_field'。

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.

我看过UserChangeForm,但看起来它已经正确地将User作为模型。我不确定为什么表单中缺少new_field。

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.

我知道创建用户配置文件的推荐方法,只是在特定情况下,我没有看到创建一个完整的新表以及在我想存储的所有内容时额外调用数据库的优点一些信息,如is_private或其他一些信息。如果我存储更多信息,那么我同意,建立用户配置文件更可取。

5 个解决方案

#1


14  

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.

首先是“它插上了吗?”问题 - 您是否手动将new_field添加到数据库中的users表中?当然,Syncdb不会处理这个问题。

After that, I would try appending the fields onto the existing UserAdmin rather than rebuilding it from scratch:

之后,我会尝试将字段附加到现有的UserAdmin上,而不是从头开始重建它:

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',)

#2


4  

It is preferred to write your own user profile class, and attach it to the User model. Then you can use the get_profile() method to retrieve the profile from the user.

最好编写自己的用户配置文件类,并将其附加到用户模型。然后,您可以使用get_profile()方法从用户检索配置文件。

Subclassing the profile admin from an Inline Admin should also allow you to edit the profile on the user's page, which is almost what you're trying to do.

从内联管理员对配置文件管理员进行子类化还应该允许您在用户页面上编辑配置文件,这几乎就是您要做的事情。

This post has a really good write-up on the issue: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/

这篇文章对这个问题有一个非常好的写作:http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/

#3


2  

Despite your DB look up concerns for having a UserProfile, that is really the way you should go for this problem. There are two main reasons for this:

尽管你的数据库看起来有关于使用UserProfile的问题,但这确实是你应该解决这个问题的方法。这有两个主要原因:

  1. The User Model is pretty tightly coupled to Django's authentication system and to many other plugins. Monkey patching it isn't a good idea.

    用户模型与Django的身份验证系统和许多其他插件紧密相关。猴子修补它不是一个好主意。

  2. Django has built-in support for creating User Profiles and many extensions know how to play nicely with these. By using User Profiles (see http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users for the main discussion on this) you can benefit from that.

    Django内置支持创建用户配置文件,许多扩展知道如何很好地使用这些。通过使用用户配置文件(请参阅http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users进行主要讨论),您可以从中受益。

If you're looking to add functionality to the Admin interface for users, then this note may be helpful:

如果您要为用户添加管理界面的功能,则此注释可能会有所帮助:

http://www.thenestedfloat.com/articles/displaying-custom-user-profile-fields-in-djangos-admin/index.html

http://www.thenestedfloat.com/articles/displaying-custom-user-profile-fields-in-djangos-admin/index.html

Regards,

问候,

Ian

伊恩

#4


2  

Just figured this out, perhaps this could help you as well.

刚想出来,也许这对你也有帮助。

Since you don't mention that you have a separate profile, if you want to simply add a column to the existing User admin, you can do the following in admin.py:

由于您没有提到您有一个单独的配置文件,如果您只想向现有用户管理员添加一列,您可以在admin.py中执行以下操作:

First you create a custom admin by subclassing UserAdmin:

首先,通过继承UserAdmin创建自定义管理员:

class CustomUserAdmin(UserAdmin):
    list_display = UserAdmin.list_display + ('is_complete',)

    def is_complete(self, obj):
        # Example here, you can use any expression.
        return SomeOtherClass.objects.get(my_field=obj).is_complete()

    # Not required, but this gives you a nice boolean field:
    is_complete.boolean = True

Then unregister the existing UserAdmin and register your own:

然后取消注册现有的UserAdmin并注册您自己的:

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

#5


0  

I got the 'that is missing from the form' error, and discovered that it was due to my field being marked as 'editable=False' in the model.

我得到了'表单中缺少的'错误,并发现这是因为我的字段在模型中被标记为'editable = False'。

#1


14  

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.

首先是“它插上了吗?”问题 - 您是否手动将new_field添加到数据库中的users表中?当然,Syncdb不会处理这个问题。

After that, I would try appending the fields onto the existing UserAdmin rather than rebuilding it from scratch:

之后,我会尝试将字段附加到现有的UserAdmin上,而不是从头开始重建它:

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',)

#2


4  

It is preferred to write your own user profile class, and attach it to the User model. Then you can use the get_profile() method to retrieve the profile from the user.

最好编写自己的用户配置文件类,并将其附加到用户模型。然后,您可以使用get_profile()方法从用户检索配置文件。

Subclassing the profile admin from an Inline Admin should also allow you to edit the profile on the user's page, which is almost what you're trying to do.

从内联管理员对配置文件管理员进行子类化还应该允许您在用户页面上编辑配置文件,这几乎就是您要做的事情。

This post has a really good write-up on the issue: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/

这篇文章对这个问题有一个非常好的写作:http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/

#3


2  

Despite your DB look up concerns for having a UserProfile, that is really the way you should go for this problem. There are two main reasons for this:

尽管你的数据库看起来有关于使用UserProfile的问题,但这确实是你应该解决这个问题的方法。这有两个主要原因:

  1. The User Model is pretty tightly coupled to Django's authentication system and to many other plugins. Monkey patching it isn't a good idea.

    用户模型与Django的身份验证系统和许多其他插件紧密相关。猴子修补它不是一个好主意。

  2. Django has built-in support for creating User Profiles and many extensions know how to play nicely with these. By using User Profiles (see http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users for the main discussion on this) you can benefit from that.

    Django内置支持创建用户配置文件,许多扩展知道如何很好地使用这些。通过使用用户配置文件(请参阅http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users进行主要讨论),您可以从中受益。

If you're looking to add functionality to the Admin interface for users, then this note may be helpful:

如果您要为用户添加管理界面的功能,则此注释可能会有所帮助:

http://www.thenestedfloat.com/articles/displaying-custom-user-profile-fields-in-djangos-admin/index.html

http://www.thenestedfloat.com/articles/displaying-custom-user-profile-fields-in-djangos-admin/index.html

Regards,

问候,

Ian

伊恩

#4


2  

Just figured this out, perhaps this could help you as well.

刚想出来,也许这对你也有帮助。

Since you don't mention that you have a separate profile, if you want to simply add a column to the existing User admin, you can do the following in admin.py:

由于您没有提到您有一个单独的配置文件,如果您只想向现有用户管理员添加一列,您可以在admin.py中执行以下操作:

First you create a custom admin by subclassing UserAdmin:

首先,通过继承UserAdmin创建自定义管理员:

class CustomUserAdmin(UserAdmin):
    list_display = UserAdmin.list_display + ('is_complete',)

    def is_complete(self, obj):
        # Example here, you can use any expression.
        return SomeOtherClass.objects.get(my_field=obj).is_complete()

    # Not required, but this gives you a nice boolean field:
    is_complete.boolean = True

Then unregister the existing UserAdmin and register your own:

然后取消注册现有的UserAdmin并注册您自己的:

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

#5


0  

I got the 'that is missing from the form' error, and discovered that it was due to my field being marked as 'editable=False' in the model.

我得到了'表单中缺少的'错误,并发现这是因为我的字段在模型中被标记为'editable = False'。