2013-12-18 38 views
6

給定一到一個擴展的模型,如Django的用戶模型中的值:訪問,從一到一個表

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") 

如何顯示在管理員?

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

近匹配問題是Django Admin: How to display value of fields with list_display from two models which are in oneToOne relation?

回答

11

的一般方法是:這裏

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

x是正在顯示

模型的對象
相關問題