Skip to content

Commit cfb95aa

Browse files
committed
Merge pull request #63 from amureki/master
Added choices field representation
2 parents 335dd4a + ece15b8 commit cfb95aa

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

reversion_compare/compare.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def _obj_repr(self, obj):
5353
except Exception as e:
5454
return repr(obj)
5555

56+
def _choices_repr(self, obj):
57+
return force_text(dict(self.field.flatchoices).get(obj, obj),
58+
strings_only=True)
59+
5660
def _to_string_ManyToManyField(self):
5761
queryset = self.get_many_to_many()
5862
return ", ".join([self._obj_repr(item) for item in queryset])
@@ -68,6 +72,9 @@ def to_string(self):
6872
func = getattr(self, func_name)
6973
return func()
7074

75+
if self.field.choices:
76+
return self._choices_repr(self.value)
77+
7178
if isinstance(self.value, str):
7279
return self.value
7380
else:

tests/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ class VariantModel(models.Model):
9898
9999
TODO: Add tests for all variants!
100100
"""
101+
102+
TEST_CHOICES = (
103+
('a', 'alpha'),
104+
('b', 'bravo'),
105+
)
101106
boolean = models.BooleanField(default=True)
102107
null_boolean = models.NullBooleanField()
103108

104109
char = models.CharField(max_length=1, blank=True, null=True)
110+
choices_char = models.CharField(max_length=1, blank=True, null=True,
111+
choices=TEST_CHOICES)
105112
text = models.TextField(blank=True, null=True)
106113
# skip: models.SlugField()
107114

tests/test_utils/test_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ def create_VariantModel_data(self):
306306
null_boolean = None,
307307

308308
char = "a",
309+
choices_char = 'a',
309310
text = "Foo 'one'",
310311
# skip: models.SlugField()
311312

@@ -340,6 +341,7 @@ def create_VariantModel_data(self):
340341
("null_boolean", True),
341342
("null_boolean", False),
342343
("char", "B"),
344+
("choices_char", "b"),
343345
("text", "Bar 'two'"),
344346
# skip: models.SlugField()
345347
("integer", -1),

tests/test_variant_model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def test_all_changes(self):
138138
"<del>- a</del>",
139139
"<ins>+ B</ins>",
140140

141+
"<h3>choices char</h3>",
142+
"<del>- alpha</del>",
143+
"<ins>+ bravo</ins>",
144+
141145
"<h3>text</h3>",
142146
"<del>- Foo &#39;one&#39;</del>",
143147
"<ins>+ Bar &#39;two&#39;</ins>",

0 commit comments

Comments
 (0)