Access from django admin list_display, a value from a One-To-One table

后端 未结 1 1789
孤独总比滥情好
孤独总比滥情好 2021-02-14 14:41

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

class UserProfile(models.Model):
     user        = models.OneToOneField(User, related_name=         


        
相关标签:
1条回答
  • 2021-02-14 15:28

    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

    0 讨论(0)
提交回复
热议问题