Ticket #3871: 3871.diff

File 3871.diff, 3.1 KB (added by EspenG, 18 years ago)
  • django/db/models/fields/related.py

    === modified file 'django/db/models/fields/related.py'
     
    388388    # some other model (rather than having a ManyToManyField themselves).
    389389    # In the example "publication.article_set", the article_set attribute is a
    390390    # ManyRelatedObjectsDescriptor instance.
    391     def __init__(self, related):
     391    def __init__(self, related, manager=None):
    392392        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__
    393397
    394398    def __get__(self, instance, instance_type=None):
    395399        if instance is None:
    396400            raise AttributeError, "Manager must be accessed via instance"
    397401
    398402        # Dynamically create a class that subclasses the related
    399403        # model's default manager.
    400404        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)
    403406
    404407        qn = backend.quote_name
    405408        manager = RelatedManager(
     
    723725        # m2m relations to self do not have a ManyRelatedObjectsDescriptor,
    724726        # as it would be redundant - unless the field is non-symmetrical.
    725727        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]))
    728738
    729739        # Set up the accessors for the column names on the m2m table
    730740        self.m2m_column_name = curry(self._get_m2m_column_name, related)
  • django/db/models/related.py

    === modified file 'django/db/models/related.py'
     
    1919        self.name = '%s:%s' % (self.opts.app_label, self.opts.module_name)
    2020        self.var_name = self.opts.object_name.lower()
    2121
     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
    2230    def flatten_data(self, follow, obj=None):
    2331        new_data = {}
    2432        rel_instances = self.get_list(obj)
Back to Top