Skip to content

Commit b04578f

Browse files
committed
Fix Python error when ManyToMany relations didn't exist.
This avoids storing an ugettext value in self.version. It's value got parsed in `ids = [int(v) for v in self.value]` causing an `int("F")` call that fails. Instead, a sentinal value is introduced that can be recognized in the code, yet output a meaningful message when it's shown to the user.
1 parent 1a09967 commit b04578f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

reversion_compare/compare.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,27 @@
1414
from django.contrib.contenttypes.models import ContentType
1515
from django.core.exceptions import ObjectDoesNotExist
1616
from django.db import models
17-
from django.utils.translation import ugettext as _
17+
from django.utils.encoding import force_text, python_2_unicode_compatible
18+
from django.utils.translation import ugettext
1819

1920
import reversion
2021
from reversion.models import has_int_pk
2122

2223
logger = logging.getLogger(__name__)
2324

2425

26+
@python_2_unicode_compatible
27+
class FieldVersionDoesNotExist(object):
28+
"""
29+
Sentinel object to handle missing fields
30+
"""
31+
32+
def __str__(self):
33+
return force_text(ugettext("Field Didn't exist!"))
34+
35+
DOES_NOT_EXIST = FieldVersionDoesNotExist()
36+
37+
2538
class CompareObject(object):
2639
def __init__(self, field, field_name, obj, version, has_int_pk, adapter):
2740
self.field = field
@@ -31,7 +44,7 @@ def __init__(self, field, field_name, obj, version, has_int_pk, adapter):
3144
self.has_int_pk = has_int_pk
3245
self.adapter = adapter
3346
# try and get a value, if none punt
34-
self.value = version.field_dict.get(field_name, _("Field Didn't exist!"))
47+
self.value = version.field_dict.get(field_name, DOES_NOT_EXIST)
3548

3649
def _obj_repr(self, obj):
3750
# FIXME: How to create a better representation of the current value?
@@ -111,6 +124,9 @@ def get_many_to_many(self):
111124
"""
112125
if self.field.get_internal_type() != "ManyToManyField": # FIXME!
113126
return ([], [], [], []) # TODO: refactory that
127+
elif self.value is DOES_NOT_EXIST:
128+
return ([], [], [], [])
129+
114130
ids = None
115131
if self.has_int_pk:
116132
ids = [int(v) for v in self.value] # is: version.field_dict[field.name]

0 commit comments

Comments
 (0)