Skip to content

Commit

Permalink
Merge pull request #167 from treyhunner/force-onetoone-to-foreignkey
Browse files Browse the repository at this point in the history
Force OneToOne to be a ForeignKey on historical models
  • Loading branch information
macro1 committed Apr 22, 2015
2 parents 062cf28 + 4d4727e commit 601592a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changes
=======

tip (unreleased)
----------------
- Fix OneToOneField transformation for historical models (gh-166)

1.6.0 (2015-04-16)
------------------
- Add support for Django 1.8+
Expand Down
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 601592a

Please sign in to comment.