Skip to content

Commit b114fec

Browse files
committed
queryset-refactor: Fixed a few inadvertent sharing problems for related fields
in abstract base classes. This means, for example, that many-to-many fields can be used in abstract base classes. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7431 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent a14135e commit b114fec

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

django/db/models/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import types
23
import sys
34
import os
@@ -111,7 +112,7 @@ def __new__(cls, name, bases, attrs):
111112
if field.name in names:
112113
raise FieldError('Local field %r in class %r clashes with field of similar name from abstract base class %r'
113114
% (field.name, name, base.__name__))
114-
new_class.add_to_class(field.name, field)
115+
new_class.add_to_class(field.name, copy.deepcopy(field))
115116

116117
if abstract:
117118
# Abstract base models can't be instantiated and don't appear in

django/db/models/fields/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import datetime
23
import os
34
import time
@@ -126,11 +127,13 @@ def __cmp__(self, other):
126127
return cmp(self.creation_counter, other.creation_counter)
127128

128129
def __deepcopy__(self, memodict):
129-
# Slight hack; deepcopy() is difficult to do on classes with
130-
# dynamically created methods. Fortunately, we can get away with doing
131-
# a shallow copy in this particular case.
132-
import copy
133-
return copy.copy(self)
130+
# We don't have to deepcopy very much here, since most things are not
131+
# intended to be altered after initial creation.
132+
obj = copy.copy(self)
133+
if self.rel:
134+
obj.rel = copy.copy(self.rel)
135+
memodict[id(self)] = obj
136+
return obj
134137

135138
def to_python(self, value):
136139
"""

0 commit comments

Comments
 (0)