Ticket #8528: ticket8528-1.2.4.after.3400.1.2.4.diff

File ticket8528-1.2.4.after.3400.1.2.4.diff, 4.3 KB (added by marcob, 4 years ago)

A newer patch I apply after the #3400 one (don't know if works with trunk, give it a try)

  • django\contrib\admin\filterspecs.

    old new  
    7676            self.lookup_title = f.verbose_name # use field name
    7777        rel_name = other_model._meta.pk.name
    7878        self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
    79         self.lookup_null_kwarg = '%s__isnull' % (self.field_path) #f.name
    8079        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    81         self.lookup_null = request.GET.get(self.lookup_null_kwarg, None)
    8280        self.lookup_choices = f.get_choices(include_blank=False)
    8381       
    8482    def has_output(self):
     
    8886        return self.lookup_title
    8987
    9088    def choices(self, cl):
    91         yield {'selected': self.lookup_val is None and not self.lookup_null,
    92                'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_null_kwarg]),
     89        yield {'selected': self.lookup_val is None,
     90               'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
    9391               'display': _('All')}
    9492        for pk_val, val in self.lookup_choices:
    9593            yield {'selected': self.lookup_val == smart_unicode(pk_val),
    96                    'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}, [self.lookup_null_kwarg]),
     94                   'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
    9795                   'display': val}
    98         if self.field.null:
    99             yield {'selected': self.lookup_null,
    100                    'query_string': cl.get_query_string({self.lookup_null_kwarg: 'True'}, [self.lookup_kwarg]),
    101                    'display': '(%s)' % _("None")}
    10296
    10397FilterSpec.register(lambda f: (
    10498        hasattr(f, 'rel') and bool(f.rel) or
     
    198192        super(AllValuesFilterSpec, self).__init__(f, request, params, model,
    199193                                                  model_admin,
    200194                                                  field_path=field_path)
    201         self.lookup_kwarg = self.field_path
    202         self.lookup_kwarg_isnull = '%s__isnull' % self.field_path
    203         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    204         self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull, None)
    205         parent_model, reverse_path = reverse_field_path(model, self.field_path)
     195        self.lookup_val = request.GET.get(self.field_path, None)
     196        parent_model, reverse_path = reverse_field_path(model, field_path)
    206197        queryset = parent_model._default_manager.all()
    207198        # optional feature: limit choices base on existing relationships
    208199        # queryset = queryset.complex_filter(
     
    211202        queryset = queryset.filter(limit_choices_to)
    212203       
    213204        self.lookup_choices = \
    214             queryset.distinct().order_by(f.name).values_list(f.name, flat=True)
     205            queryset.distinct().order_by(f.name).values(f.name)
    215206
    216207    def title(self):
    217208        return self.field.verbose_name
    218209
    219210    def choices(self, cl):
    220         yield {'selected': self.lookup_val is None and self.lookup_val_isnull is None,
    221                'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]),
     211        yield {'selected': self.lookup_val is None,
     212               'query_string': cl.get_query_string({}, [self.field_path]),
    222213               'display': _('All')}
    223         include_none = False
    224        
    225214        for val in self.lookup_choices:
    226             if val is None:
    227                 include_none = True
    228                 continue
    229             val = smart_unicode(val)
    230            
     215            val = smart_unicode(val[self.field.name])
    231216            yield {'selected': self.lookup_val == val,
    232                    'query_string': cl.get_query_string({self.lookup_kwarg: val}, [self.lookup_kwarg_isnull]),
     217                   'query_string': cl.get_query_string({self.field_path: val}),
    233218                   'display': val}
    234         if include_none:
    235             yield {'selected': self.lookup_val_isnull is not None,
    236                     'query_string': cl.get_query_string({self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]),
    237                     'display': _('None')}
    238 
    239219FilterSpec.register(lambda f: True, AllValuesFilterSpec)
Back to Top