| 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 | |
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) |