Ticket #3400: list_filter.2.diff

File list_filter.2.diff, 2.0 KB (added by nick@…, 18 years ago)

Improved patch

  • 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 not new_field.rel: break
     587                    field = new_field
     588                self.verbose_name = field.verbose_name
     589                self.rel = field.rel
     590                # Rejoin the lookups using the separator so that the
     591                # RelatedFilterSpec uses the correct lookup_kwarg.
     592                self.name = LOOKUP_SEPARATOR.join(lookups)
     593
    575594        filter_specs = []
    576595        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:
     596            for field_name in self.lookup_opts.admin.list_filter:
     597                field_lookups = field_name.split(LOOKUP_SEPARATOR)
     598                f = self.lookup_opts.get_field(field_lookups[0])
     599                # If the field name spans multiple models, wrap the field in
     600                # a FakeForeignKey.
     601                if len(field_lookups) > 1 and f.rel:
     602                    f = FakeForeignKey(f, field_lookups)
    580603                spec = FilterSpec.create(f, request, self.params, self.model)
    581604                if spec and spec.has_output():
    582605                    filter_specs.append(spec)
Back to Top