从django管理员list_display访问,这是一对一表中的值 [英] Access from django admin list_display, a value from a One-To-One table

查看:262
本文介绍了从django管理员list_display访问,这是一对一表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出模型的一对一扩展,例如Django User模型:

Given a one-to-one extension a model, such as the Django User model:

class UserProfile(models.Model):
     user        = models.OneToOneField(User, related_name='profile', unique=True)
     avatar      = models.ImageField(_('Avatar'))
     foo         = models.CharField(max_length=100, verbose_name="xxx")

如何在管理员中显示该内容?

How can I display that in the admin?

class UserAdmin(admin.ModelAdmin):
     list_display = ('email', 'profile__foo' <--NOT WORKING )

接近比赛的问题是推荐答案

一般方法是:

class UseAdmin(admin.ModelAdmin):
    list_display = ('email', 'profile_foo')
    def profile_foo(self, x):
        return x.profile.foo
    profile_foo.short_description = 'foo'

x是您要显示的模型的对象

x here is the object of the model you are displaying

这篇关于从django管理员list_display访问,这是一对一表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆