Skip to content

Commit 5cab094

Browse files
Ensure object_to is updated from self if required
1 parent b520816 commit 5cab094

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Authors
4040
- Michael England
4141
- Gregory Bataille
4242
- Jesse Shapiro
43+
- Kevin Foster
4344

4445
Background
4546
==========

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Unreleased
55
----------
66
- Use get_queryset rather than model.objects in history_view. (gh-303)
77
- Change ugettext calls in models.py to ugettext_lazy
8+
- Resolve issue where model references itself (gh-278)
89

910
1.9.0 (2017-06-11)
1011
------------------

simple_history/models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,17 @@ def copy_fields(self, model):
172172
field_arguments['to_field'] = old_field.to_fields[0]
173173
if getattr(old_field, 'db_column', None):
174174
field_arguments['db_column'] = old_field.db_column
175+
176+
# If old_field.rel.to is 'self' then we have a case where object has a foreign key
177+
# to itself. In this case we update need to set the `to` value of the field
178+
# to be set to a model. We can use the old_field.model value.
179+
if isinstance(old_field.rel.to, str) and old_field.rel.to == 'self':
180+
object_to = old_field.model
181+
else:
182+
object_to = old_field.rel.to
183+
175184
field = FieldType(
176-
old_field.rel.to,
185+
object_to,
177186
related_name='+',
178187
null=True,
179188
blank=True,

0 commit comments

Comments
 (0)