Skip to content

Commit d6ef1d1

Browse files
author
Rod Xavier Bondoc
committed
Add support for django < 1.8
1 parent 411f946 commit d6ef1d1

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

simple_history/models.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,29 @@ def copy_fields(self, model):
124124
for field in model._meta.fields:
125125
field = copy.copy(field)
126126
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
127146
if isinstance(field, OrderWrt):
128147
# OrderWrt is a proxy field, switch to a plain IntegerField
129148
field.__class__ = models.IntegerField
130-
if isinstance(field, models.ForeignKey):
131-
# Don't allow reverse relations.
132-
# ForeignKey knows best what datatype to use for the column
133-
# we'll used that as soon as it's finalized by copying rel.to
134-
old_field = field
135-
field = type(field)(field.rel.to, related_name='+', null=True, blank=True)
136-
field.rel = old_field.rel
137-
field.rel.related_name = '+'
138-
field.name = old_field.name
139-
field.db_constraint = False
140-
field._unique = False
141-
else:
142-
transform_field(field)
149+
transform_field(field)
143150
fields[field.name] = field
144151
return fields
145152

0 commit comments

Comments
 (0)