Given a one-to-one extension a model, such as the Django User model:
给定一对一扩展模型,例如Django User模型:
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 )
A near match question is Django Admin: How to display value of fields with list_display from two models which are in oneToOne relation?
一个近乎匹配的问题是Django Admin:如何用oneToOne关系中的两个模型显示list_display的字段值?
1 个解决方案
#1
11
The general method would be:
一般方法是:
class UseAdmin(admin.ModelAdmin):
list_display = ('email', 'profile_foo')
def profile_foo(self, x):
return x.profile.foo
profile_foo.short_description = 'foo'
x here is the object of the model you are displaying
x此处是您正在显示的模型的对象
#1
11
The general method would be:
一般方法是:
class UseAdmin(admin.ModelAdmin):
list_display = ('email', 'profile_foo')
def profile_foo(self, x):
return x.profile.foo
profile_foo.short_description = 'foo'
x here is the object of the model you are displaying
x此处是您正在显示的模型的对象