2016-08-02 7 views
1

Django認証ユーザーモデルとカスタムユーザープロファイルモデルを使用しています。ユーザープロファイルの管理者は、次のようになります。カスタム管理者リストの表示にDjangoの認証ユーザーフィールドを表示する方法

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    first_login = models.BooleanField(default = True) 
    TYPE_CHOICES = (('R', 'Reader'), ('A', 'Author')) 
    type = models.CharField(max_length = 9, choices = TYP_CHOICES, blank = True) 
    project = models.ForeignKey('Project', on_delete=models.CASCADE) 

私は何をしたいことのリスト表示で、ユーザのis_activeプロパティを表示するには、次のとおりです。

class UserProfileAdmin(admin.ModelAdmin): 
    list_display = ['user', 'first_login', 'project', 'type'] 
    class Meta: 
     model = UserProfile 

ユーザプロファイルモデルは次のようになりますUserProfileAdmin。これは可能ですか?はいの場合、どうですか?

答えて

1

あなたのようなシグネチャを持つカスタム管理モデルでwrapped_is_active方法と言う定義する場合、それは可能です:あなたはそれがあるので、あなたのlist_displayでその方法を指定する必要があり

def wrapped_is_active(self, item): 
    if item: 
     return item.user.is_active 
wrapped_is_active.boolean = True 

のような次のようになります。

list_display=['user', 'first_login', 'project', 'type', 'wrapped_is_active'] 

詳細についてはDjango admin site documentation

+0

作品:)しかし、なぜ 'user__is_active'のような検索がうまくいかないのですか? –

+0

私はよく分かりませんが、この場合、管理者は 'queryset'データに基づいてビューを構築しているようです。属性をチェックし、ifが呼び出し可能な場合はそれを呼び出して値を取得します。 adminの 'get_queryset'メソッドで遊ぶことができるかもしれません。しかし間違いなくそれは二重チェックされるべきです。 –

+0

'list_display'が' user__is_active'のような外部キーの属性を扱えるようにするためのチケット[#5863](https://code.djangoproject.com/ticket/5863)がありましたが、 " – Alasdair

0

それは可能です:あなたのコードに変更を加えました:

class UserProfileAdmin(admin.ModelAdmin): 
    list_display = ['user', 'first_login', 'project', 'type','is_active'] 
    class Meta: 
     model = UserProfile 


class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    first_login = models.BooleanField(default = True) 
    TYPE_CHOICES = (('R', 'Reader'), ('A', 'Author')) 
    type = models.CharField(max_length = 9, choices = TYP_CHOICES, blank = True) 
    project = models.ForeignKey('Project', on_delete=models.CASCADE) 
is_active = models.BooleanField(default=True) 
関連する問題