Skip to content

Commit

Permalink
Merge 5503893 into e3ff030
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhitlock committed Nov 17, 2014
2 parents e3ff030 + 5503893 commit 058e2d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
13 changes: 11 additions & 2 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apps = None
from django.db import models, router
from django.db.models import loading
from django.db.models.fields.proxy import OrderWrt
from django.db.models.fields.related import RelatedField
from django.db.models.related import RelatedObject
from django.conf import settings
Expand Down Expand Up @@ -135,6 +136,12 @@ def copy_fields(self, model):
field.rel.related_name = '+'
field.null = True
field.blank = True
if isinstance(field, OrderWrt):
# Don't include the OrderWrt proxy field. This is added when
# the Meta.order_with_respect_to option is enabled on the
# model. It causes issues with Django 1.7 migrations, and
# there's no clean way of restoring historical order.
continue
transform_field(field)
fields[field.name] = field
return fields
Expand Down Expand Up @@ -208,7 +215,8 @@ def create_historical_record(self, instance, history_type):
manager = getattr(instance, self.manager_name)
attrs = {}
for field in instance._meta.fields:
attrs[field.attname] = getattr(instance, field.attname)
if not isinstance(field, OrderWrt):
attrs[field.attname] = getattr(instance, field.attname)
manager.create(history_date=history_date, history_type=history_type,
history_user=history_user, **attrs)

Expand Down Expand Up @@ -347,5 +355,6 @@ def __init__(self, model):

def __get__(self, instance, owner):
values = (getattr(instance, f.attname)
for f in self.model._meta.fields)
for f in self.model._meta.fields
if not isinstance(f, OrderWrt))
return self.model(*values)
15 changes: 15 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,18 @@ class Meta:
class CustomFKError(models.Model):
fk = models.ForeignKey(SecondLevelInheritedModel)
history = HistoricalRecords()


class Series(models.Model):
"""A series of works, like a trilogy of books."""
name = models.CharField(max_length=100)
author = models.CharField(max_length=100)


class SeriesWork(models.Model):
series = models.ForeignKey('Series', related_name='works')
title = models.CharField(max_length=100)
history = HistoricalRecords()

class Meta:
order_with_respect_to = 'series'
13 changes: 12 additions & 1 deletion simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Person, FileModel, Document, Book, HistoricalPoll, Library, State,
AbstractBase, ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
HistoricalState, HistoricalCustomFKError
HistoricalState, HistoricalCustomFKError, Series
)
from ..external.models import ExternalModel2, ExternalModel4

Expand Down Expand Up @@ -281,6 +281,17 @@ def test_historical_verbose_name_follows_model_verbose_name(self):
self.assertEqual('historical quiet please',
l.history.get()._meta.verbose_name)

def test_ordered_with_respect_to_omitted(self):
s = Series.objects.create(
name="The Hitchhiker's Guide to the Galaxy Trilogy",
author="Douglas Adams")
w4 = s.works.create(title="So Long, and Thanks for All the Fish")
history = w4.history.get()

self.assertTrue(hasattr(w4, '_order'))
self.assertFalse(hasattr(history, '_order'))
self.assertIsNone(history.history_object._order)


class RegisterTest(TestCase):
def test_register_no_args(self):
Expand Down

0 comments on commit 058e2d1

Please sign in to comment.