简体   繁体   English

在admin.py中扩展Django用户模型后,如何显示用户名之类的内容

[英]How to display things such as username when youve extended the Django User Model in admin.py

so I expanded on the Django User Model and this is what I currently have: 所以我扩展了Django用户模型,这是我目前拥有的:

class StudentProfile(models.Model):
    user = models.OneToOneField(User, null = True, related_name = 'StudentProfile', on_delete = models.CASCADE)
    teacher = models.BooleanField(default = False)
    school = models.CharField(max_length = 50)
    #replies = models.ManyToManyField()


class TeacherProfile(models.Model):
    user = models.OneToOneField(User, null = True, related_name = 'TeacherProfile', on_delete = models.CASCADE)
    teacher = models.BooleanField(default = True)
    school = models.CharField(max_length = 100)
    subject = models.CharField(max_length = 100)
    head_of_subject = models.BooleanField(default = False)
    headmaster = models.BooleanField(default = False)
    #reviews = models.ManyToManyField()

class SchoolProfile(models.Model):
    school_name = models.CharField(max_length = 100, default = '')
    identification_code = models.CharField(max_length = 10, unique = True)
    teachers = models.ManyToManyField(TeacherProfile, blank = True)

I am now setting up the admin.py and I was wondering how i could display things such as the users first, last, and username? 我现在正在设置admin.py,我想知道如何显示诸如用户名,姓氏和用户名之类的内容?

This is what I have tried, however it has not worked. 这是我尝试过的方法,但是没有奏效。

class StudentProfileAdmin(admin.ModelAdmin):
    list_display = ["user.username", "school"]

    class Meta:
        model = StudentProfile

class TeacherProfileAdmin(admin.ModelAdmin):
    list_display = ["user.username", "school"]

    class Meta:
        model = TeacherProfile

class SchoolProfileAdmin(admin.ModelAdmin):
    list_display = ["school_name", "identification_code"]

    class Meta:
        model = SchoolProfile

admin.site.register(StudentProfile, StudentProfileAdmin)

admin.site.register(TeacherProfile, TeacherProfileAdmin)
admin.site.register(SchoolProfile, SchoolProfileAdmin)

Thanks for any help! 谢谢你的帮助!

You can add a function to the modeladmin to return the related user's username 您可以向modeladmin添加函数以返回相关用户的用户名

class StudentProfileAdmin(admin.ModelAdmin):
    list_display = ["username", "school"]

    def username(self, obj):
        return obj.user.username

    class Meta:
        model = StudentProfile

Edit: It also works with functions defined on the model. 编辑:它也与模型上定义的功能一起使用。 So you could add the username function to the model if you'd like to keep your admin.py cleaner. 因此,如果您想使admin.py更加整洁,可以将username功能添加到模型中。

class StudentProfile(models.Model):
    user = models.OneToOneField(User, null = True, related_name = 'StudentProfile',
                                on_delete = models.CASCADE)
    teacher = models.BooleanField(default = False)
    school = models.CharField(max_length = 50)

    @propery
    def username(self):
        return self.user.username

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

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