Ticket #5833: ticket-5833-against-newforms-admin-6477.diff

File ticket-5833-against-newforms-admin-6477.diff, 3.6 KB (added by Honza Král, 11 years ago)
  • django/contrib/admin/filterspecs.py

    diff --git a/django/contrib/admin/filterspecs.py b/django/contrib/admin/filterspecs.py
    index 1ca10ed..e15c758 100644
    a b class FilterSpec(object): 
    1919        self.field_path = field_path or f.name
    2020
    2121    def register(cls, test, factory):
    22         cls.filter_specs.append((test, factory))
     22        cls.filter_specs.insert(0, (test, factory))
    2323    register = classmethod(register)
    2424
    2525    def create(cls, f, request, params, model, model_admin, field_path=None):
    class RelatedFilterSpec(FilterSpec): 
    7777                   'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
    7878                   'display': val}
    7979
    80 FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
    81 
    8280class ChoicesFilterSpec(FilterSpec):
    8381    def __init__(self, f, request, params, model, model_admin, field_path=None):
    8482        super(ChoicesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
    class ChoicesFilterSpec(FilterSpec): 
    9492                    'query_string': cl.get_query_string({self.lookup_kwarg: k}),
    9593                    'display': v}
    9694
    97 FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
    98 
    9995class DateFieldFilterSpec(FilterSpec):
    10096    def __init__(self, f, request, params, model, model_admin, field_path=None):
    10197        super(DateFieldFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
    class DateFieldFilterSpec(FilterSpec): 
    129125                   'query_string': cl.get_query_string(param_dict, [self.field_generic]),
    130126                   'display': title}
    131127
    132 FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)
    133 
    134128class BooleanFieldFilterSpec(FilterSpec):
    135129    def __init__(self, f, request, params, model, model_admin, field_path=None):
    136130        super(BooleanFieldFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
    class BooleanFieldFilterSpec(FilterSpec): 
    152146                   'query_string': cl.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]),
    153147                   'display': _('Unknown')}
    154148
    155 FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec)
    156 
    157 # This should be registered last, because it's a last resort. For example,
    158 # if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
    159 # more appropriate, and the AllValuesFilterSpec won't get used for it.
    160149class AllValuesFilterSpec(FilterSpec):
    161150    def __init__(self, f, request, params, model, model_admin, field_path=None):
    162151        super(AllValuesFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path)
    class AllValuesFilterSpec(FilterSpec): 
    176165            yield {'selected': self.lookup_val == val,
    177166                   'query_string': cl.get_query_string({self.field_path: val}),
    178167                   'display': val}
     168
     169# This should be registered first, because it's a last resort. For example,
     170# if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
     171# more appropriate, and the AllValuesFilterSpec won't get used for it.
    179172FilterSpec.register(lambda f: True, AllValuesFilterSpec)
     173FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec)
     174FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)
     175FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
     176FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
Back to Top