LoginSignup
1
1

More than 5 years have passed since last update.

ModelAdmin.list_filter: 関連する条件で 絞り込む

Posted at
  • Permission 一覧を ContentType自体と、ContentType.app_labelで絞る
  • ContentType.app_labelを指定したら、関連するContentTypeだけ選択肢に表示したい

CorelatedFilter: 関連フィルター

  • admin.RelatedFieldListFilterベース
  • 関連フィールド(cofield)がクエリパラメータにあったら、 limit_choices_to を指定する
from django.contrib import admin    


class CorelatedFilter(admin.RelatedFieldListFilter):                                

    def field_choices(self, field, request, model_admin):                           
        name = "{}__{}".format(field.name, self.cofield)                            
        covalue = request.GET.get(name, '')                                         
        if not covalue:                                                             
            return field.get_choices(include_blank=False)                           
        return field.get_choices(                                                   
            include_blank=False,                                                    
            limit_choices_to={self.cofield: covalue})                               

PermissionAdmin

  • コンテントタイプと、その app_label で絞る
class PermissionAdmin(admin.ModelAdmin):                                         
    list_filter = [                                                              
        'content_type__app_label',                                               
        ('content_type',                                                         
         type('', (CorelatedFilter,), {'cofield': 'app_label'})),                
    ]                                                                            
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1