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