Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force OneToOne to be a ForeignKey on historical models #167

Merged
merged 1 commit into from
Apr 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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