Skip to content

Commit

Permalink
fix model + tests for foreign keys (issue #38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien Nozay committed May 7, 2013
1 parent cb14abe commit febf43b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions simple_history/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
from django.db import models
from django.db.models.loading import get_model
from django.conf import settings
from django.contrib import admin
from django.utils import importlib
Expand Down Expand Up @@ -84,10 +85,21 @@ def copy_fields(self, model):
field.__class__ = models.TextField

if isinstance(field, models.ForeignKey):
field.__class__ = models.IntegerField
rel_model = field.rel.to
field.name = field.get_attname()
if isinstance(field.rel.to, basestring):
rel_model = get_model(*field.rel.to.split('.',1))

if isinstance(rel_model._meta.pk, models.fields.AutoField):
field.__class__ = models.IntegerField
else:
rel_field = rel_model._meta.pk
field.__class__ = type(rel_field)
field.max_length = rel_field.max_length
#ughhhh. open to suggestions here
field.rel = None
field.related = None
field.related_name = None
field.related_query_name = None
field.null = True
field.blank = True
Expand All @@ -106,8 +118,6 @@ def copy_fields(self, model):
field._unique = False
field.db_index = True
field.serialize = True
if fk:
field.name = field.name + "_id"
fields[field.name] = field

return fields
Expand Down
4 changes: 2 additions & 2 deletions simple_history/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ def test_non_default_primary_key_save(self):
library.save()
library.book = None
library.save()
self.assertEqual([l.book for l in library.history.all()],
[None, book2, book1])
self.assertEqual([l.book_id for l in library.history.all()],
[None, book2.isbn, book1.isbn])


def test_raw_save(self):
Expand Down

0 comments on commit febf43b

Please sign in to comment.