2012-03-14 17 views
3

別のフィルタで行われた現在の選択に応じて、1つのフィルタ変更のプロンプトを表示しようとしています。私は、AttributeFilterに渡されたAttributeCategoryFilterの現在選択されている値を取得する方法についてはかなり失われています。私はDjango 1.4-devを使用しています。この目的でRelatedFieldListFilterを使用するべきかどうかを調べようとしています。これらの機能は、まだ野生の中に浮遊している例はないほど若いです(m)。現在のフィルタ選択をDjangoの別のカスタムSimpleListFilterに渡す

class AttributeCategoryFilter(SimpleListFilter): 
     title = _('Attribute Category') 
     parameter_name = 'attribute_category' 
     def lookups(self, request, model_admin): 
      attributes = Attribute.objects.filter(parent_attribute=None) 
      prompts = [] 
      for attribute in attributes: 
       prompts.append((attribute.title, _(str(attribute.title)))) 
      return prompts 
     def queryset(self, request, queryset): 
      if self.value(): 
       return queryset.filter(attribute__category=self.value()) 
      else: 
       return queryset 


    class AttributeFilter(SimpleListFilter): 
     title = _('Attribute Title') 
     parameter_name = 'attribute_title' 
     def lookups(self, request, model_admin): 
      desired_category = # Needs to be a reference to the selected value in the AttributeCategoryFilter above 
      attributes = Attribute.objects.filter(category=desired_category).exclude(parent_attribute=None) 
      prompts = [] 
      for attribute in attributes: 
       prompts.append((attribute.title, _(str(attribute.title)))) 
      return prompts 
     def queryset(self, request, queryset): 
      if self.value(): 
       return queryset.filter(attribute__title=self.value()) 
      else: 
       return queryset 


    class ValueAdmin(admin.ModelAdmin): 
     list_display = ('package', 'attribute', 'presence', 'text', 'modified', 'created') 
     list_filter = ('package', AttributeCategoryFilter, AttributeFilter, 'presence', 
      'attribute__admin_approved', 'attribute__dtype', 'modified') 
     search_fields = ('package', 'attribute', 'text') 
     list_display_links = ('package',) 
     list_editable = ('presence', 'text') 
     list_per_page = 20000 
    admin.site.register(Value, ValueAdmin) 
+0

1つの選択肢は、要求から取得することです。 –

答えて

1

私は何か他のものを探していました。これはダン・klassonがあまりにも提案するものであるように

def lookups(self, request, model_admin): 
    game_id = request.GET.get('game', None) 
    players = Player.objects.all() 
    if game_id: 
     players = players.filter(character__game_id=game_id) 
    return [(p.id, p.__unicode__()) for p in players] 

が見える:

は、ここで私は選択されたゲーム内の文字を持っていた唯一のショーの選手にやったことです。

ヒント:クエリのパラメータにIDを設定することは、一般的にセキュリティ上の理由からno-noとみなされます。

2

ここで私はうまくいきました。 "TypeListFilter"は、 "Category"フィルタが使用されると表示され、選択したカテゴリの "subTypeOf"であるすべてのエントリを表示します。 「特別なケース」のハックをさらに下に押すと、ユーザが別のカテゴリを選択したときにフィルタが消えることが保証されます。 "_class"パラメータは、いくつかの柔軟性を追加します。私は異なるが関連するタイプクラスと同じフィルタを使用しているだけで、この1つのパラメータをオーバーライドする必要があります。フィルタリングするadmin.Modelクラスで置き換えるだけです。

class TypeListFilter(admin.SimpleListFilter): 
    """ 
    Provide filter for DnaComponentType (the actual "second level" type). 

    This filter has one cosmetic problem, which is that it's setting is not 
    automatically deleted if the category filter is changed. I tried but the 
    request and queryset are all immutable. Instead, the queryset method is 
    checking for any missmatch between category and filter name and filtering 
    is ignored if the category name doesn't match the current subType name. 
    """ 
    title = 'Type' 
    parameter_name = 'type' 

    _class = None 

    def lookups(self, request, model_admin): 
     """ 
     Returns a list of tuples. The first element in each 
     tuple is the coded value for the option that will 
     appear in the URL query. The second element is the 
     human-readable name for the option that will appear 
     in the right sidebar. 
     """ 
     if not u'category' in request.GET: 
      return() 

     category_name = request.GET[u'category'] 
     types = self._class.objects.filter(subTypeOf__name=category_name) 
     return ((t.name, t.name) for t in types) 

    def queryset(self, request, queryset): 
     """ 
     Returns the filtered queryset based on the value 
     provided in the query string and retrievable via 
     `self.value()`. 
     """ 
     if not u'category' in request.GET: 
      return queryset 

     category = request.GET[u'category'] 
     subtypes = self._class.objects.filter(subTypeOf__name=category) 

     r = queryset.filter(componentType__subTypeOf__name=category) 

     if not self.value(): 
      return r 

     ## special case: missmatch between subtype and category 
     ## which happens after switching the category 
     if len(subtypes.filter(name=self.value())) == 0: 
      return r 

     return r.filter(componentType__name=self.value()) 
関連する問題