简体   繁体   English

如何访问由 admin.py 中的 OnetoOneField 引用的另一个 model 扩展的“用户”model 的属性

[英]How to access attributes of the 'User' model extended by another model referenced by a OnetoOneField in admin.py

In model.py, I have a class extending the User model在 model.py 中,我有一个 class 扩展用户 model

class Tutor(models.Model):
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)

In admin.py, I tried to access user.id with 'user_id', which works在 admin.py 中,我尝试使用“user_id”访问 user.id,这很有效

class TutorAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'contact_number')

在此处输入图像描述

However, when I try to access other attributes of 'user' such as username, email with this format但是,当我尝试使用这种格式访问“用户”的其他属性时,例如用户名 email

class TutorAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'user_username', 'contact_number')

This occurs出现这种情况在此处输入图像描述

What is the format to access attributes such as user.username or user.email?访问 user.username 或 user.email 等属性的格式是什么?

UPDATE更新

Solved it with解决了

class TutorAdmin(admin.ModelAdmin):
    @admin.display(description='username')
    def username(self):
        u = self.user
        if u is not None:
            return u.username

    @admin.display(description='email')
    def email(self):
        u = self.user
        if u is not None:
            return u.email


    list_display = ('user_id', username, email, 'contact_number')

Thanks to the solution by Willem Van Onsem感谢Willem Van Onsem的解决方案

You can define methods and reference these in the list_display :您可以定义方法并在list_display中引用它们:

class TutorAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'contact_number', username)

    @admin.display(description='username')
    def username(self, obj):
        u = obj.user
        if u is not None:
            return u.username

You can however not edit the usernames, since the function defines how to access the data, not how to alter it.但是,您不能编辑用户名,因为 function 定义了如何访问数据,而不是如何更改数据。

Since the __str__ for the builtin User model returns the username, you can also work with:由于内置User __str__的 __str__ 返回用户名,您还可以使用:

class TutorAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'contact_number', user)

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

相关问题 在admin.py中扩展Django用户模型后,如何显示用户名之类的内容 - How to display things such as username when youve extended the Django User Model in admin.py 如何在admin.py中访问当前用户 - How to access current user in admin.py 在admin.py中将一个模型的数据显示到另一个模型中 - Display data of one model into another in admin.py 如何在dajango的自定义用户模型中修复admin.py中不可调用的属性错误? - how to fix not a callable attribute error in admin.py in custom user model in dajango? 如何更新通过“OneToOneField”扩展的用户模型 - How can I update user model extended via "OneToOneField" 将所有注册用户从Django的内置用户模型转换为admin.py中的类 - Getting all registered users from Django's inbuilt user model into a class within admin.py 可以根据 DRF admin.py 中的字段拆分 model - possible to split the model based on field in DRF admin.py 在 admin.py 文件 (Django) 中注册 model 时出现问题 - Problem while registering a model in admin.py file (Django) 我如何在覆盖 save_model() function 的同时在 admin.py 中使用验证? - How do i use validation in admin.py while overriding save_model() function? Django 1.8.5。 如何使用get_language()在我的应用程序的admin.py中的Model.Admin中定义用户语言? - Django 1.8.5. How can I define the user language in Model.Admin in the admin.py of my app using get_language()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM