mirror of
https://github.com/django/django.git
synced 2025-10-26 07:06:08 +00:00
Fixed handling of multiple fields in a model pointing to the same related model.
Thanks to ElliotM, mk and oyvind for some excellent test cases for this. Fixed #7110, #7125. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7778 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -44,6 +44,7 @@ class Options(object):
|
||||
self.one_to_one_field = None
|
||||
self.abstract = False
|
||||
self.parents = SortedDict()
|
||||
self.duplicate_targets = {}
|
||||
|
||||
def contribute_to_class(self, cls, name):
|
||||
from django.db import connection
|
||||
@@ -115,6 +116,24 @@ class Options(object):
|
||||
auto_created=True)
|
||||
model.add_to_class('id', auto)
|
||||
|
||||
# Determine any sets of fields that are pointing to the same targets
|
||||
# (e.g. two ForeignKeys to the same remote model). The query
|
||||
# construction code needs to know this. At the end of this,
|
||||
# self.duplicate_targets will map each duplicate field column to the
|
||||
# columns it duplicates.
|
||||
collections = {}
|
||||
for column, target in self.duplicate_targets.iteritems():
|
||||
try:
|
||||
collections[target].add(column)
|
||||
except KeyError:
|
||||
collections[target] = set([column])
|
||||
self.duplicate_targets = {}
|
||||
for elt in collections.itervalues():
|
||||
if len(elt) == 1:
|
||||
continue
|
||||
for column in elt:
|
||||
self.duplicate_targets[column] = elt.difference(set([column]))
|
||||
|
||||
def add_field(self, field):
|
||||
# Insert the given field in the order in which it was created, using
|
||||
# the "creation_counter" attribute of the field.
|
||||
|
||||
Reference in New Issue
Block a user