6
6
from django .apps import apps # Django >= 1.7
7
7
except ImportError :
8
8
apps = None
9
- from django .db import models
9
+ from django .db import models , router
10
10
from django .db .models .fields .related import RelatedField
11
11
from django .db .models .related import RelatedObject
12
12
from django .conf import settings
@@ -53,8 +53,6 @@ def python_2_unicode_compatible(klass):
53
53
54
54
registered_models = {}
55
55
56
- nonrel_dbs = ('django_mongodb_engine' )
57
-
58
56
59
57
class HistoricalRecords (object ):
60
58
thread = threading .local ()
@@ -267,11 +265,7 @@ def get_field(self, other, cls):
267
265
if isinstance (to_field , models .OneToOneField ):
268
266
field = self .get_one_to_one_field (to_field , other )
269
267
elif isinstance (to_field , models .AutoField ):
270
- # Check if AutoField is string for django-non-rel support
271
- if settings .DATABASES ['default' ]['ENGINE' ] in nonrel_dbs :
272
- field .__class__ = models .TextField
273
- else :
274
- field .__class__ = models .IntegerField
268
+ field .__class__ = convert_auto_field (to_field )
275
269
else :
276
270
field .__class__ = to_field .__class__
277
271
excluded_prefixes = ("_" , "__" )
@@ -323,13 +317,7 @@ def transform_field(field):
323
317
"""Customize field appropriately for use in historical model"""
324
318
field .name = field .attname
325
319
if isinstance (field , models .AutoField ):
326
- # The historical model gets its own AutoField, so any
327
- # existing one must be replaced with an IntegerField.
328
- if settings .DATABASES ['default' ]['ENGINE' ] in nonrel_dbs :
329
- # Check if AutoField is string for django-non-rel support
330
- field .__class__ = models .TextField
331
- else :
332
- field .__class__ = models .IntegerField
320
+ field .__class__ = convert_auto_field (field )
333
321
334
322
elif isinstance (field , models .FileField ):
335
323
# Don't copy file, just path.
@@ -348,6 +336,19 @@ def transform_field(field):
348
336
field .serialize = True
349
337
350
338
339
+ def convert_auto_field (field ):
340
+ """Convert AutoField to a non-incrementing type
341
+
342
+ The historical model gets its own AutoField, so any existing one
343
+ must be replaced with an IntegerField.
344
+ """
345
+ connection = router .db_for_write (field .model )
346
+ if settings .DATABASES [connection ]['ENGINE' ] in ('django_mongodb_engine' ,):
347
+ # Check if AutoField is string for django-non-rel support
348
+ return models .TextField
349
+ return models .IntegerField
350
+
351
+
351
352
class HistoricalObjectDescriptor (object ):
352
353
def __init__ (self , model ):
353
354
self .model = model
0 commit comments