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
@@ -268,7 +268,7 @@ def get_field(self, other, cls):
268
268
if isinstance (to_field , models .OneToOneField ):
269
269
field = self .get_one_to_one_field (to_field , other )
270
270
elif isinstance (to_field , models .AutoField ):
271
- field .__class__ = models . IntegerField
271
+ field .__class__ = convert_auto_field ( to_field )
272
272
else :
273
273
field .__class__ = to_field .__class__
274
274
excluded_prefixes = ("_" , "__" )
@@ -320,9 +320,8 @@ def transform_field(field):
320
320
"""Customize field appropriately for use in historical model"""
321
321
field .name = field .attname
322
322
if isinstance (field , models .AutoField ):
323
- # The historical model gets its own AutoField, so any
324
- # existing one must be replaced with an IntegerField.
325
- field .__class__ = models .IntegerField
323
+ field .__class__ = convert_auto_field (field )
324
+
326
325
elif isinstance (field , models .FileField ):
327
326
# Don't copy file, just path.
328
327
field .__class__ = models .TextField
@@ -340,6 +339,19 @@ def transform_field(field):
340
339
field .serialize = True
341
340
342
341
342
+ def convert_auto_field (field ):
343
+ """Convert AutoField to a non-incrementing type
344
+
345
+ The historical model gets its own AutoField, so any existing one
346
+ must be replaced with an IntegerField.
347
+ """
348
+ connection = router .db_for_write (field .model )
349
+ if settings .DATABASES [connection ]['ENGINE' ] in ('django_mongodb_engine' ,):
350
+ # Check if AutoField is string for django-non-rel support
351
+ return models .TextField
352
+ return models .IntegerField
353
+
354
+
343
355
class HistoricalObjectDescriptor (object ):
344
356
def __init__ (self , model ):
345
357
self .model = model
0 commit comments