Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions reversion_compare/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def _obj_repr(self, obj):
except Exception as e:
return repr(obj)

def _choices_repr(self, obj):
return force_text(dict(self.field.flatchoices).get(obj, obj),
strings_only=True)

def _to_string_ManyToManyField(self):
queryset = self.get_many_to_many()
return ", ".join([self._obj_repr(item) for item in queryset])
Expand All @@ -68,6 +72,9 @@ def to_string(self):
func = getattr(self, func_name)
return func()

if self.field.choices:
return self._choices_repr(self.value)

if isinstance(self.value, str):
return self.value
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ class VariantModel(models.Model):

TODO: Add tests for all variants!
"""

TEST_CHOICES = (
('a', 'alpha'),
('b', 'bravo'),
)
boolean = models.BooleanField(default=True)
null_boolean = models.NullBooleanField()

char = models.CharField(max_length=1, blank=True, null=True)
choices_char = models.CharField(max_length=1, blank=True, null=True,
choices=TEST_CHOICES)
text = models.TextField(blank=True, null=True)
# skip: models.SlugField()

Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def create_VariantModel_data(self):
null_boolean = None,

char = "a",
choices_char = 'a',
text = "Foo 'one'",
# skip: models.SlugField()

Expand Down Expand Up @@ -340,6 +341,7 @@ def create_VariantModel_data(self):
("null_boolean", True),
("null_boolean", False),
("char", "B"),
("choices_char", "b"),
("text", "Bar 'two'"),
# skip: models.SlugField()
("integer", -1),
Expand Down
4 changes: 4 additions & 0 deletions tests/test_variant_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def test_all_changes(self):
"<del>- a</del>",
"<ins>+ B</ins>",

"<h3>choices char</h3>",
"<del>- alpha</del>",
"<ins>+ bravo</ins>",

"<h3>text</h3>",
"<del>- Foo &#39;one&#39;</del>",
"<ins>+ Bar &#39;two&#39;</ins>",
Expand Down