简体   繁体   中英

Django - How can I change profile view in admin's panel?

Well, I created a model Profile:

class Profile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.PROTECT,
    )
    website = models.URLField(blank=True)
    bio = models.CharField(max_length=240, blank=True)

And added it to admin.py:

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    model = Profile

So, I have user's panel on my admin's panel. But every profile in the list of profiles are called 'profile object(1)', 'profile object(2)' and etc, but when I tap on the link, I can see the user's name. So, how can I can change the view of profiles in the list of profiles so they are called there as user is called?

How it looks like in admin's panel它在管理员面板中的样子

How it looks like inside the profile配置文件内部的样子

And how I want it to look like (modified with F12): 在此处输入图片说明

EDITED: How my str looks like (commented, because doesn't work):

# __str__ - более удобное отображение в админке
#    def __str__(self):
#         return self.user_username

Fix your __str__()

def __str__(self):
    return self.user.username

这是#admin.py 中的另一个解决方案,您可以编写list_display = ["user__username",]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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