Ticket #1722: exclude_in_admin.diff
File exclude_in_admin.diff, 6.2 KB (added by , 19 years ago) |
---|
-
django/core/management.py
897 897 else: 898 898 for fn in opts.admin.list_filter: 899 899 try: 900 f = opts.get_field(fn )900 f = opts.get_field(fn.lstrip('-')) 901 901 except models.FieldDoesNotExist: 902 902 e.add(opts, '"admin.list_filter" refers to %r, which isn\'t a field.' % fn) 903 903 -
django/contrib/admin/filterspecs.py
11 11 12 12 class FilterSpec(object): 13 13 filter_specs = [] 14 def __init__(self, f, request, params):14 def __init__(self, f, exclude_filter, request, params): 15 15 self.field = f 16 self.exclude_filter = exclude_filter 16 17 self.params = params 17 18 18 19 def register(cls, test, factory): 19 20 cls.filter_specs.append( (test, factory) ) 20 21 register = classmethod(register) 21 22 22 def create(cls, f, request, params):23 def create(cls, f, exclude_filter, request, params): 23 24 for test, factory in cls.filter_specs: 24 25 if test(f): 25 return factory(f, request, params)26 return factory(f, exclude_filter, request, params) 26 27 create = classmethod(create) 27 28 28 29 def has_output(self): … … 42 43 for choice in self.choices(cl): 43 44 t.append('<li%s><a href="%s">%s</a></li>\n' % \ 44 45 ((choice['selected'] and ' class="selected"' or ''), 45 choice['query_string'],46 (self.exclude_filter and join('-', choice['query_string']) or join('-', choice['query_string'])), 46 47 choice['display'])) 47 48 t.append('</ul>\n\n') 48 49 return "".join(t) 49 50 50 51 class RelatedFilterSpec(FilterSpec): 51 def __init__(self, f, request, params):52 super(RelatedFilterSpec, self).__init__(f, request, params)52 def __init__(self, f, exclude_filter, request, params): 53 super(RelatedFilterSpec, self).__init__(f, exclude_filter, request, params) 53 54 if isinstance(f, models.ManyToManyField): 54 55 self.lookup_title = f.rel.to._meta.verbose_name 55 56 else: 56 57 self.lookup_title = f.verbose_name 57 self.lookup_kwarg = '%s __%s__exact' % (f.name, f.rel.to._meta.pk.name)58 self.lookup_kwarg = '%s%s__%s__exact' % (self.exclude_filter and '-' or '', f.name, f.rel.to._meta.pk.name) 58 59 self.lookup_val = request.GET.get(self.lookup_kwarg, None) 59 60 self.lookup_choices = f.rel.to._default_manager.all() 60 61 … … 77 78 FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec) 78 79 79 80 class ChoicesFilterSpec(FilterSpec): 80 def __init__(self, f, request, params):81 super(ChoicesFilterSpec, self).__init__(f, request, params)82 self.lookup_kwarg = '%s __exact' % f.name81 def __init__(self, f, exclude_filter, request, params): 82 super(ChoicesFilterSpec, self).__init__(f, exclude_filter, request, params) 83 self.lookup_kwarg = '%s%s__exact' % (self.exclude_filter and '-' or '', f.name) 83 84 self.lookup_val = request.GET.get(self.lookup_kwarg, None) 84 85 85 86 def choices(self, cl): … … 94 95 FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec) 95 96 96 97 class DateFieldFilterSpec(FilterSpec): 97 def __init__(self, f, request, params):98 super(DateFieldFilterSpec, self).__init__(f, request, params)99 98 def __init__(self, f, exclude_filter, request, params): 99 super(DateFieldFilterSpec, self).__init__(f, exclude_filter, request, params) 100 100 101 self.field_generic = '%s__' % self.field.name 101 102 102 103 self.date_params = dict([(k, v) for k, v in params.items() if k.startswith(self.field_generic)]) … … 129 130 FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec) 130 131 131 132 class BooleanFieldFilterSpec(FilterSpec): 132 def __init__(self, f, request, params): 133 super(BooleanFieldFilterSpec, self).__init__(f, request, params) 133 def __init__(self, f, exclude_filter, request, params): 134 135 136 137 super(BooleanFieldFilterSpec, self).__init__(f, exclude_filter, request, params) 134 138 self.lookup_kwarg = '%s__exact' % f.name 135 139 self.lookup_kwarg2 = '%s__isnull' % f.name 136 140 self.lookup_val = request.GET.get(self.lookup_kwarg, None) -
django/contrib/admin/views/main.py
569 569 def get_filters(self, request): 570 570 filter_specs = [] 571 571 if self.lookup_opts.admin.list_filter and not self.opts.one_to_one_field: 572 filter_fields = [ self.lookup_opts.get_field(field_name) \572 filter_fields = [(self.lookup_opts.get_field(field_name.lstrip('-')), field_name.startswith('-')) \ 573 573 for field_name in self.lookup_opts.admin.list_filter] 574 for f in filter_fields:575 spec = FilterSpec.create(f, request, self.params)574 for f, exclude_filter in filter_fields: 575 spec = FilterSpec.create(f, exclude_filter, request, self.params) 576 576 if spec and spec.has_output(): 577 577 filter_specs.append(spec) 578 578 return filter_specs, bool(filter_specs) … … 669 669 del lookup_params[i] 670 670 671 671 # Apply lookup parameters from the query string. 672 qs = qs.filter(**lookup_params) 672 filter_params={} 673 exclude_params={} 674 for k, v in lookup_params.iteritems(): 675 if k.startswith('-'): 676 exclude_params[k.lstrip('-')]=v 677 else: 678 filter_params[k]=v 679 680 qs = qs.filter(**filter_params) 681 qs = qs.exclude(**exclude_params) 673 682 674 683 # Use select_related() if one of the list_display options is a field 675 684 # with a relationship.