简体   繁体   English

如何设置Django Admin在“用户个人资料”标签中显示所有用户的属性?

[英]How to set Django Admin to show all User's attributes in his User Profile tab?

I'm working on a Django project. 我正在研究Django项目。 I have two types of members (user profiles) - Customer and Translator. 我有两种类型的成员(用户个人资料)-客户和翻译。 I would like to see those profiles in a Django Admin including it's User attributes like username , surename etc. 我想在Django Admin中看到这些配置文件,包括它的User属性,如usernamesurename等。

Now, when I create let's say UserTranslatorProfile called Thomas , I can see his name, username etc. in Users in Auth in Django Admin and the other attributes which belongs to it's profile in UserTranslatorProfiles which is very uncomfortable. 现在,当我创建假设UserTranslatorProfile叫Thomas ,我可以看到他的名字,用户名等UsersAuthDjango Admin和属于它在配置文件中的其他属性UserTranslatorProfiles这是非常不舒服。 (In UserTranslatorProfile I can see which user it is but I can't see his attributes and can't change them). (在UserTranslatorProfile我可以看到它是哪个用户,但看不到他的属性,也无法更改它们)。

The two profiles are here: 这两个配置文件在这里:

class UserTranslatorProfile(models.Model):
    user = models.OneToOneField(User)
    languages = models.ManyToManyField(Language)

class UserCustomerProfile(models.Model):
    user = models.OneToOneField(User)

And they are of course registered in admin.py 他们当然是在admin.py注册的

So the question is: Is it possible and if, how to see all User and UserProfile attibutes by clicking on UserProfile in Django-Admin ? 因此,问题是:是否有可能,以及如何通过在Django-Admin单击UserProfile来查看所有UserUserProfile属性?

Add a ModelAdmin for UserTranslatorProfiles and add the username , email and other related fields into the fields attribute. UserTranslatorProfiles添加一个ModelAdmin ,并将usernameemail和其他相关字段添加到fields属性中。

ModelAdmin are put in the admin.py file and they further customize the way models are displayed in the admin. ModelAdmin放在admin.py文件中,它们进一步自定义模型在admin中的显示方式。

    from django.contrib import admin
    from myproject.myapp.models import UserTranslatorProfiles # Import your model

    class UserTranslatorProfilesAdmin(admin.ModelAdmin):
        fields = ('username', 'email, ) # Enter full list of fields here

    admin.site.register(UserTranslatorProfiles, UserTranslatorProfilesAdmin)

Just need to set ModelAdmin and register it with admin site 只需设置ModelAdmin并将其注册到管理站点

step 1: create a admin.py in your application if not exist. step 1:在应用程序中创建admin.py(如果不存在)。
step-2: import your model you want to show on admin site. step-2:导入要在管理网站上显示的模型。
step-3: Create a admin model and register it with admin site. step-3:创建一个管理模型,并在管理站点中注册。

from django.contrib import admin
from myproject.myapp.models import Author #import your model
class AuthorAdmin(admin.ModelAdmin):
      fields = ('name', 'title', 'view_birth_date')# define-fields


admin.site.register(Author, AuthorAdmin) #now register your custom-admin model

for more 更多
https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#modeladmin-objects https://docs.djangoproject.com/zh-CN/1.9/ref/contrib/admin/#modeladmin-objects

Unfortunately this is more difficult than it needs to be/should be. 不幸的是,这比需要/应该要困难得多。 Without an inline admin, the admin add/change forms are only built to edit one model. 没有内联管理员,管理员添加/更改表单仅用于编辑一个模型。

If you're able to change your user model, you can accomplish this by creating your own custom User object that inherits from User. 如果能够更改用户模型,则可以通过创建自己的自定义User对象(继承自User)来实现。 Then, in settings.py, set AUTH_USER_MODEL to your new model. 然后,在settings.py中,将AUTH_USER_MODEL设置为新模型。 Note that this should be done before data has been input, and your first migrations have been done (so you'd need to start fresh.) 请注意,此操作应在输入数据和完成首次迁移之前完成(因此,您需要重新开始。)

If you're unable to do so, I can think of two other directions you could take: 如果您无法执行此操作,那么我可以考虑采取另外两个方向:

Easier: Create an inline admin for the User object, and register that to your translation object. 更容易:为User对象创建一个内联admin,并将其注册到您的翻译对象。 Then you'll have a single row in an inline admin at the bottom of the page, which can be used to edit the User. 然后,在页面底部的内联管理员中将有一行,可用于编辑用户。 I hate this from an end-user UX perspective, but it could get the job done. 从最终用户UX的角度来看,我讨厌这样做,但是它可以完成工作。

Neater: Create a custom form, add fields that correspond to those that you'd like to edit in the User model, and then in the form's clean and save methods, validate/save the methods of the underlying User object. 简洁:创建一个自定义表单,添加与您要在User模型中编辑的字段相对应的字段,然后在表单的clean和save方法中验证/保存基础User对象的方法。 For example, create the form fields "tra_username", "tra_firstname", "tra_password"... Then, in the form's save method (after validation), do this for each field 例如,创建表单字段“ tra_username”,“ tra_firstname”,“ tra_password” ...然后,在表单的save方法中(验证后),对每个字段执行此操作

UserCustomProfile.user.first_name = form.cleaned_data.get("tra_firstname")`
UserCustomProfile.user.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM