|
2 | 2 |
|
3 | 3 | import threading
|
4 | 4 | import copy
|
5 |
| -try: |
6 |
| - from django.apps import apps |
7 |
| -except ImportError: # Django < 1.7 |
8 |
| - apps = None |
| 5 | +import warnings |
| 6 | + |
9 | 7 | from django.db import models, router
|
10 | 8 | from django.db.models import loading
|
11 | 9 | from django.db.models.fields.proxy import OrderWrt
|
12 | 10 | from django.db.models.fields.related import RelatedField
|
13 |
| -from django.db.models.related import RelatedObject |
14 | 11 | from django.conf import settings
|
15 | 12 | from django.contrib import admin
|
16 | 13 | from django.utils import importlib, six
|
17 | 14 | from django.utils.encoding import python_2_unicode_compatible
|
18 | 15 | from django.utils.encoding import smart_text
|
19 | 16 | from django.utils.timezone import now
|
20 | 17 | from django.utils.translation import string_concat
|
| 18 | + |
| 19 | +from .manager import HistoryDescriptor |
| 20 | + |
| 21 | +try: |
| 22 | + from django.apps import apps |
| 23 | +except ImportError: # Django < 1.7 |
| 24 | + apps = None |
21 | 25 | try:
|
22 | 26 | from south.modelsinspector import add_introspection_rules
|
23 | 27 | except ImportError: # south not present
|
24 | 28 | pass
|
25 | 29 | else: # south configuration for CustomForeignKeyField
|
26 | 30 | add_introspection_rules(
|
27 | 31 | [], ["^simple_history.models.CustomForeignKeyField"])
|
28 |
| -from .manager import HistoryDescriptor |
29 | 32 |
|
30 | 33 | registered_models = {}
|
31 | 34 |
|
@@ -121,18 +124,25 @@ def copy_fields(self, model):
|
121 | 124 | for field in model._meta.fields:
|
122 | 125 | field = copy.copy(field)
|
123 | 126 | field.rel = copy.copy(field.rel)
|
124 |
| - if isinstance(field, models.ForeignKey): |
125 |
| - # Don't allow reverse relations. |
126 |
| - # ForeignKey knows best what datatype to use for the column |
127 |
| - # we'll used that as soon as it's finalized by copying rel.to |
128 |
| - field.__class__ = CustomForeignKeyField |
129 |
| - field.rel.related_name = '+' |
130 |
| - field.null = True |
131 |
| - field.blank = True |
132 | 127 | if isinstance(field, OrderWrt):
|
133 | 128 | # OrderWrt is a proxy field, switch to a plain IntegerField
|
134 | 129 | field.__class__ = models.IntegerField
|
135 |
| - transform_field(field) |
| 130 | + if isinstance(field, models.ForeignKey): |
| 131 | + old_field = field |
| 132 | + field = type(field)( |
| 133 | + field.rel.to, |
| 134 | + related_name='+', |
| 135 | + null=True, |
| 136 | + blank=True, |
| 137 | + primary_key=False, |
| 138 | + db_index=True, |
| 139 | + serialize=True, |
| 140 | + ) |
| 141 | + field._unique = False |
| 142 | + field.name = old_field.name |
| 143 | + field.db_constraint = False |
| 144 | + else: |
| 145 | + transform_field(field) |
136 | 146 | fields[field.name] = field
|
137 | 147 | return fields
|
138 | 148 |
|
@@ -227,6 +237,8 @@ def get_history_user(self, instance):
|
227 | 237 | class CustomForeignKeyField(models.ForeignKey):
|
228 | 238 |
|
229 | 239 | def __init__(self, *args, **kwargs):
|
| 240 | + warnings.warn("CustomForeignKeyField is deprecated.", |
| 241 | + DeprecationWarning) |
230 | 242 | super(CustomForeignKeyField, self).__init__(*args, **kwargs)
|
231 | 243 | self.db_constraint = False
|
232 | 244 | self.generate_reverse_relation = False
|
@@ -289,13 +301,6 @@ def get_field(self, other, cls):
|
289 | 301 |
|
290 | 302 | def do_related_class(self, other, cls):
|
291 | 303 | field = self.get_field(other, cls)
|
292 |
| - if not hasattr(self, 'related'): |
293 |
| - try: |
294 |
| - instance_type = cls.instance_type |
295 |
| - except AttributeError: # when model is reconstituted for migration |
296 |
| - pass # happens during migrations |
297 |
| - else: |
298 |
| - self.related = RelatedObject(other, instance_type, self) |
299 | 304 | transform_field(field)
|
300 | 305 | field.rel = None
|
301 | 306 |
|
|
0 commit comments