=== modified file 'django/db/models/fields/related.py'
|
|
|
388 | 388 | # some other model (rather than having a ManyToManyField themselves). |
389 | 389 | # In the example "publication.article_set", the article_set attribute is a |
390 | 390 | # ManyRelatedObjectsDescriptor instance. |
391 | | def __init__(self, related): |
| 391 | def __init__(self, related, manager=None): |
392 | 392 | self.related = related # RelatedObject instance |
| 393 | if manager == None: |
| 394 | self.super_manager = related.model._default_manager.__class__ |
| 395 | else: |
| 396 | self.super_manager = manager.__class__ |
393 | 397 | |
394 | 398 | def __get__(self, instance, instance_type=None): |
395 | 399 | if instance is None: |
396 | 400 | raise AttributeError, "Manager must be accessed via instance" |
397 | 401 | |
398 | 402 | # Dynamically create a class that subclasses the related |
399 | 403 | # model's default manager. |
400 | 404 | rel_model = self.related.model |
401 | | superclass = rel_model._default_manager.__class__ |
402 | | RelatedManager = create_many_related_manager(superclass) |
| 405 | RelatedManager = create_many_related_manager(self.super_manager) |
403 | 406 | |
404 | 407 | qn = backend.quote_name |
405 | 408 | manager = RelatedManager( |
… |
… |
|
723 | 725 | # m2m relations to self do not have a ManyRelatedObjectsDescriptor, |
724 | 726 | # as it would be redundant - unless the field is non-symmetrical. |
725 | 727 | if related.model != related.parent_model or not self.rel.symmetrical: |
726 | | # Add the descriptor for the m2m relation |
727 | | setattr(cls, related.get_accessor_name(), ManyRelatedObjectsDescriptor(related)) |
| 728 | related_accessor_name = related.get_accessor_name() |
| 729 | remote_accessors = related.get_remote_accessors() |
| 730 | |
| 731 | # Add the descriptor for the m2m relation, using the default manager |
| 732 | # AND keeping backwards compatibility |
| 733 | setattr(cls, related_accessor_name, ManyRelatedObjectsDescriptor(related)) |
| 734 | |
| 735 | # Addin accessors with different managers |
| 736 | for racc in remote_accessors: |
| 737 | setattr(cls, "%s__%s" % (related_accessor_name, racc[0]), ManyRelatedObjectsDescriptor(related, racc[1])) |
728 | 738 | |
729 | 739 | # Set up the accessors for the column names on the m2m table |
730 | 740 | self.m2m_column_name = curry(self._get_m2m_column_name, related) |
=== modified file 'django/db/models/related.py'
|
|
|
19 | 19 | self.name = '%s:%s' % (self.opts.app_label, self.opts.module_name) |
20 | 20 | self.var_name = self.opts.object_name.lower() |
21 | 21 | |
| 22 | def get_remote_accessors(self): |
| 23 | d = [] |
| 24 | from django.db.models.manager import ManagerDescriptor |
| 25 | for name, attr in self.model.__dict__.items(): |
| 26 | if isinstance(attr, ManagerDescriptor): |
| 27 | d.append((name, attr.manager)) |
| 28 | return d |
| 29 | |
22 | 30 | def flatten_data(self, follow, obj=None): |
23 | 31 | new_data = {} |
24 | 32 | rel_instances = self.get_list(obj) |