Skip to content

Commit b1d9adb

Browse files
yakimkaRoss Mechanic
authored andcommitted
Fixed error when trying to get instance if object is deleted #571 (#574)
* Fixed error when trying to get instance if object is deleted #571 * fixed DoesNotExist bug #571 and updated tests * updated CHANGES.rst * updated AUTHORS.rst * Don't pass excluded fields to instance if object is deleted. Rewrited tests
1 parent 7b1a47a commit b1d9adb

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Authors
8787
- Trey Hunner (`treyhunner <https://github.com/treyhunner>`_)
8888
- Ulysses Vilela
8989
- `vnagendra <https://github.com/vnagendra>`_
90+
- `yakimka <https://github.com/yakimka>`_
9091

9192
Background
9293
==========

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
Unreleased
5+
----------
6+
- Fixed DoesNotExist error when trying to get instance if object is deleted (gh-571)
7+
48
2.7.3 (2019-07-15)
59
------------------
610
- Fixed BigAutoField not mirrored as BigInt (gh-556)

simple_history/models.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.conf import settings
1212
from django.contrib import admin
1313
from django.contrib.auth import get_user_model
14+
from django.core.exceptions import ObjectDoesNotExist
1415
from django.core.serializers import serialize
1516
from django.db import models
1617
from django.db.models import Q
@@ -378,12 +379,16 @@ def get_instance(self):
378379
model._meta.get_field(field).attname
379380
for field in self._history_excluded_fields
380381
]
381-
values = (
382-
model.objects.filter(pk=getattr(self, model._meta.pk.attname))
383-
.values(*excluded_attnames)
384-
.get()
385-
)
386-
attrs.update(values)
382+
try:
383+
values = (
384+
model.objects.filter(pk=getattr(self, model._meta.pk.attname))
385+
.values(*excluded_attnames)
386+
.get()
387+
)
388+
except ObjectDoesNotExist:
389+
pass
390+
else:
391+
attrs.update(values)
387392
return model(**attrs)
388393

389394
def get_next_record(self):

simple_history/tests/models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import datetime
34
import uuid
45
from django.apps import apps
56
from django.conf import settings
@@ -35,6 +36,25 @@ class PollWithExcludeFields(models.Model):
3536
history = HistoricalRecords(excluded_fields=["pub_date"])
3637

3738

39+
class PollWithExcludedFieldsWithDefaults(models.Model):
40+
question = models.CharField(max_length=200)
41+
pub_date = models.DateTimeField("date published")
42+
expiration_time = models.DateField(default=datetime.date(2030, 12, 12))
43+
place = models.TextField(null=True)
44+
min_questions = models.PositiveIntegerField(default=1)
45+
max_questions = models.PositiveIntegerField()
46+
47+
history = HistoricalRecords(
48+
excluded_fields=[
49+
"pub_date",
50+
"expiration_time",
51+
"place",
52+
"min_questions",
53+
"max_questions",
54+
]
55+
)
56+
57+
3858
class PollWithExcludedFKField(models.Model):
3959
question = models.CharField(max_length=200)
4060
pub_date = models.DateTimeField("date published")

simple_history/tests/tests/test_models.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
Poll,
7676
PollInfo,
7777
PollWithExcludeFields,
78+
PollWithExcludedFieldsWithDefaults,
7879
PollWithExcludedFKField,
7980
PollWithHistoricalIPAddress,
8081
Province,
@@ -1186,6 +1187,29 @@ def test_restore_pollwithexclude(self):
11861187
self.assertEqual(original.pub_date, poll.pub_date)
11871188

11881189

1190+
class ExcludeFieldsForDeletedObjectTest(TestCase):
1191+
def setUp(self):
1192+
self.poll = PollWithExcludedFieldsWithDefaults.objects.create(
1193+
question="what's up?", pub_date=today, max_questions=12
1194+
)
1195+
self.historical = self.poll.history.order_by("pk")[0]
1196+
self.poll.delete()
1197+
1198+
def test_restore_deleted_poll_exclude_fields(self):
1199+
original = self.historical.instance
1200+
# pub_date don't have default value so it will be None
1201+
self.assertIsNone(original.pub_date)
1202+
# same for max_questions
1203+
self.assertIsNone(original.max_questions)
1204+
1205+
def test_restore_deleted_poll_exclude_fields_with_defaults(self):
1206+
poll = self.poll
1207+
original = self.historical.instance
1208+
self.assertEqual(original.expiration_time, poll.expiration_time)
1209+
self.assertEqual(original.place, poll.place)
1210+
self.assertEqual(original.min_questions, poll.min_questions)
1211+
1212+
11891213
class ExcludeForeignKeyTest(TestCase):
11901214
def setUp(self):
11911215
self.poll = PollWithExcludedFKField.objects.create(

0 commit comments

Comments
 (0)