Skip to content

Commit

Permalink
Force OneToOne to be a ForeignKey on historical models
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Apr 21, 2015
1 parent 062cf28 commit dae2206
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import warnings

import django
from django.db import models, router
from django.db.models import loading
from django.db.models.fields.proxy import OrderWrt
Expand Down Expand Up @@ -129,18 +130,26 @@ def copy_fields(self, model):
field.__class__ = models.IntegerField
if isinstance(field, models.ForeignKey):
old_field = field
field = type(field)(
field.rel.to,
field_arguments = {}
if (getattr(old_field, 'one_to_one', False) or
isinstance(old_field, models.OneToOneField)):
FieldType = models.ForeignKey
else:
FieldType = type(old_field)
if django.get_version() >= "1.6":
field_arguments['db_constraint'] = False
field = FieldType(
old_field.rel.to,
related_name='+',
null=True,
blank=True,
primary_key=False,
db_index=True,
serialize=True,
unique=False,
**field_arguments
)
field._unique = False
field.name = old_field.name
field.db_constraint = False
else:
transform_field(field)
fields[field.name] = field
Expand Down

0 comments on commit dae2206

Please sign in to comment.