Ticket #14549: patch_commit_00e95a65d316.patch

File patch_commit_00e95a65d316.patch, 4.3 KB (added by Akis Kesoglou, 11 years ago)

updated patch for django@9db4271bd11ac23a5a565

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 3c022d6fccef48202747efd91c81ba255caec2c8..d6cc3262b19911617892f80ac683531c435f9797 100644
    a b class OneToOneRel(ManyToOneRel):  
    12431243
    12441244class ManyToManyRel(object):
    12451245    def __init__(self, to, related_name=None, limit_choices_to=None,
    1246                  symmetrical=True, through=None, db_constraint=True, related_query_name=None):
     1246                 symmetrical=True, through=None, through_fields=None,
     1247                 db_constraint=True, related_query_name=None):
    12471248        if through and not db_constraint:
    12481249            raise ValueError("Can't supply a through model and db_constraint=False")
     1250        if through_fields and not through:
     1251            raise ValueError("Can't specify through_fields without a through model")
    12491252        self.to = to
    12501253        self.related_name = related_name
    12511254        self.related_query_name = related_query_name
    class ManyToManyRel(object):  
    12551258        self.symmetrical = symmetrical
    12561259        self.multiple = True
    12571260        self.through = through
     1261        self.through_fields = through_fields
    12581262        self.db_constraint = db_constraint
    12591263
    12601264    def is_hidden(self):
    class ManyToManyField(RelatedField):  
    18441848            limit_choices_to=kwargs.pop('limit_choices_to', None),
    18451849            symmetrical=kwargs.pop('symmetrical', to == RECURSIVE_RELATIONSHIP_CONSTANT),
    18461850            through=kwargs.pop('through', None),
     1851            through_fields=kwargs.pop('through_fields', None),
    18471852            db_constraint=db_constraint,
    18481853        )
    18491854
    class ManyToManyField(RelatedField):  
    19411946                    for field in self.rel.through._meta.fields)
    19421947                seen_to = sum(to_model == getattr(field.rel, 'to', None)
    19431948                    for field in self.rel.through._meta.fields)
     1949                through_fields = self.rel.through_fields
    19441950
    1945                 if seen_from > 1:
     1951                if seen_from > 1 and not (through_fields and through_fields[0]):
    19461952                    errors.append(
    19471953                        checks.Error(
    19481954                            ('The model is used as an intermediary model by '
    class ManyToManyField(RelatedField):  
    19561962                        )
    19571963                    )
    19581964
    1959                 if seen_to > 1:
     1965                if seen_to > 1 and not (through_fields and through_fields[1]):
    19601966                    errors.append(
    19611967                        checks.Error(
    19621968                            ('The model is used as an intermediary model by '
    class ManyToManyField(RelatedField):  
    20542060        cache_attr = '_m2m_%s_cache' % attr
    20552061        if hasattr(self, cache_attr):
    20562062            return getattr(self, cache_attr)
     2063        source_field = self.rel.through_fields[0] if self.rel.through_fields else None
    20572064        for f in self.rel.through._meta.fields:
    2058             if hasattr(f, 'rel') and f.rel and f.rel.to == related.model:
     2065            if hasattr(f, 'rel') and f.rel and f.rel.to == related.model and (
     2066                    source_field is None or source_field == f.name):
    20592067                setattr(self, cache_attr, getattr(f, attr))
    20602068                return getattr(self, cache_attr)
    20612069
    class ManyToManyField(RelatedField):  
    20652073        if hasattr(self, cache_attr):
    20662074            return getattr(self, cache_attr)
    20672075        found = False
     2076        target_field = self.rel.through_fields[1] if self.rel.through_fields else None
    20682077        for f in self.rel.through._meta.fields:
    20692078            if hasattr(f, 'rel') and f.rel and f.rel.to == related.parent_model:
    2070                 if related.model == related.parent_model:
     2079                if target_field is None and related.model == related.parent_model:
    20712080                    # If this is an m2m-intermediate to self,
    20722081                    # the first foreign key you find will be
    20732082                    # the source column. Keep searching for
    class ManyToManyField(RelatedField):  
    20772086                        break
    20782087                    else:
    20792088                        found = True
    2080                 else:
     2089                elif target_field is None or target_field == f.name:
    20812090                    setattr(self, cache_attr, getattr(f, attr))
    20822091                    break
    20832092        return getattr(self, cache_attr)
Back to Top