|
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 |
| -try: |
14 |
| - from django.db.models.fields.related import RelatedObject |
15 |
| -except ImportError: |
16 |
| - pass |
17 | 11 | from django.conf import settings
|
18 | 12 | from django.contrib import admin
|
19 | 13 | from django.utils import importlib, six
|
20 | 14 | from django.utils.encoding import python_2_unicode_compatible
|
21 | 15 | from django.utils.encoding import smart_text
|
22 | 16 | from django.utils.timezone import now
|
23 | 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 |
| 25 | +try: |
| 26 | + from django.db.models.fields.related import RelatedObject |
| 27 | +except ImportError: |
| 28 | + RelatedObject = None |
24 | 29 | try:
|
25 | 30 | from south.modelsinspector import add_introspection_rules
|
26 | 31 | except ImportError: # south not present
|
27 | 32 | pass
|
28 | 33 | else: # south configuration for CustomForeignKeyField
|
29 | 34 | add_introspection_rules(
|
30 | 35 | [], ["^simple_history.models.CustomForeignKeyField"])
|
31 |
| -from .manager import HistoryDescriptor |
32 | 36 |
|
33 | 37 | registered_models = {}
|
34 | 38 |
|
@@ -124,29 +128,25 @@ def copy_fields(self, model):
|
124 | 128 | for field in model._meta.fields:
|
125 | 129 | field = copy.copy(field)
|
126 | 130 | field.rel = copy.copy(field.rel)
|
127 |
| - if isinstance(field, models.ForeignKey): |
128 |
| - if not 'RelatedObject' in globals(): |
129 |
| - old_field = field |
130 |
| - field = type(field)(field.rel.to, related_name='+', null=True, blank=True) |
131 |
| - field.rel = old_field.rel |
132 |
| - field.rel.related_name = '+' |
133 |
| - field.name = old_field.name |
134 |
| - field.db_constraint = False |
135 |
| - field._unique = False |
136 |
| - setattr(field, 'attname', field.name) |
137 |
| - else: |
138 |
| - # Don't allow reverse relations. |
139 |
| - # ForeignKey knows best what datatype to use for the column |
140 |
| - # we'll used that as soon as it's finalized by copying rel.to |
141 |
| - # Django < 1.8 |
142 |
| - field.__class__ = CustomForeignKeyField |
143 |
| - field.rel.related_name = '+' |
144 |
| - field.null = True |
145 |
| - field.blank = True |
146 | 131 | if isinstance(field, OrderWrt):
|
147 | 132 | # OrderWrt is a proxy field, switch to a plain IntegerField
|
148 | 133 | field.__class__ = models.IntegerField
|
149 |
| - transform_field(field) |
| 134 | + if isinstance(field, models.ForeignKey): |
| 135 | + old_field = field |
| 136 | + field = type(field)( |
| 137 | + field.rel.to, |
| 138 | + related_name='+', |
| 139 | + null=True, |
| 140 | + blank=True, |
| 141 | + primary_key=False, |
| 142 | + db_index=True, |
| 143 | + serialize=True, |
| 144 | + ) |
| 145 | + field._unique = False |
| 146 | + field.name = old_field.name |
| 147 | + field.db_constraint = False |
| 148 | + else: |
| 149 | + transform_field(field) |
150 | 150 | fields[field.name] = field
|
151 | 151 | return fields
|
152 | 152 |
|
@@ -241,6 +241,8 @@ def get_history_user(self, instance):
|
241 | 241 | class CustomForeignKeyField(models.ForeignKey):
|
242 | 242 |
|
243 | 243 | def __init__(self, *args, **kwargs):
|
| 244 | + warnings.warn("CustomForeignKeyField is deprecated.", |
| 245 | + DeprecationWarning) |
244 | 246 | super(CustomForeignKeyField, self).__init__(*args, **kwargs)
|
245 | 247 | self.db_constraint = False
|
246 | 248 | self.generate_reverse_relation = False
|
|
0 commit comments