Ticket #3400: list_filter.diff

File list_filter.diff, 2.1 KB (added by nick@…, 13 years ago)

Patch for list_filter options

  • main.py

     
    572572        self.pk_attname = self.lookup_opts.pk.attname
    573573
    574574    def get_filters(self, request):
     575        from django.db.models.query import LOOKUP_SEPARATOR
     576        class FakeForeignKey(object):
     577            """
     578            A class to represent a ForeignKey that spans multiple foreign keys
     579            over multiple model classes.
     580            """
     581            def __init__(self, field, lookups):
     582                # Traverse the relationships to reach the final model class.
     583                for field_name in lookups[1:]:
     584                    model = field.rel.to
     585                    new_field = model._meta.get_field(field_name)
     586                    if new_field.rel:
     587                        field = new_field
     588                    else:
     589                        pass
     590                self.verbose_name = field.verbose_name
     591                self.rel = field.rel
     592                # Rejoin the lookups using the separator so that the
     593                # RelatedFilterSpec uses the correct lookup_kwarg.
     594                self.name = LOOKUP_SEPARATOR.join(lookups)
     595
    575596        filter_specs = []
    576597        if self.lookup_opts.admin.list_filter and not self.opts.one_to_one_field:
    577             filter_fields = [self.lookup_opts.get_field(field_name) \
    578                               for field_name in self.lookup_opts.admin.list_filter]
    579             for f in filter_fields:
     598            for field_name in self.lookup_opts.admin.list_filter:
     599                field_lookups = field_name.split(LOOKUP_SEPARATOR)
     600                f = self.lookup_opts.get_field(field_lookups[0])
     601                # If the field name spans multiple models, wrap the field in
     602                # a FakeForeignKey.
     603                if len(field_lookups) > 1 and f.rel:
     604                    f = FakeForeignKey(f, field_lookups)
    580605                spec = FilterSpec.create(f, request, self.params, self.model)
    581606                if spec and spec.has_output():
    582607                    filter_specs.append(spec)
Back to Top