2016-05-24 2 views
1

У меня есть следующий ListFilter:Django администратора, как сохранить параметры запроса на следующий фильтр

class ProductCountryListFilter(admin.SimpleListFilter): 
    title = _('country') 
    parameter_name = 'country' 

    def lookups(self, request, model_admin): 
     unused = request.GET.get('unused', 0) 
     _countries = Product.objects.values_list('loc_country_code', flat=True).order_by(
     'loc_country_code').distinct() 
     return [(c, countries[c]) for c in _countries] 

    def queryset(self, request, queryset): 
     country = request.GET.get('country') 

     return queryset if country is None else queryset.filter(
      loc_country_code=country 
     ) 

Выход в HTML для этого является:

<a href="?country=DE">Deutschland</a> 

Я хочу добавить к HREF параметр 'неиспользуемый', поэтому в основном я хочу этот результат:

<a href="?country=DE&unused=0">Deutschland</a> 

ответ

0
def choices(self, cl): 
     yield { 
      'selected': self.value() is None, 
      'query_string': cl.get_query_string({}, [self.parameter_name]), 
      'display': _('All'), 
     } 
     for lookup, title in self.lookup_choices: 
      yield { 
       'selected': self.value() == force_text(lookup), 
       'query_string': cl.get_query_string({ 
       self.parameter_name: lookup, 
       }, []), 
       'display': title, 
      } 

Этот метод от SimpleListFilter отвечает за создание URL-адресов.

Это HTML для фильтров:

https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/filter.html

Смежные вопросы