2013-12-18 8 views
6

otrzymał jeden-na-jeden rozszerzenie modelu, takich jak Django modelu użytkownika:Dostęp z django admina list_display wartość z tabeli One-to-One

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

Jak mogę wyświetlać, że w administrator?

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

Bliska zgodność pytanie Django Admin: How to display value of fields with list_display from two models which are in oneToOne relation?

Odpowiedz

11

Ogólny sposób byłoby:

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

x Oto obiekt modelu, które jest wyświetlane

Powiązane problemy