Skip to content

Commit

Permalink
Add JSONField support (#119)
Browse files Browse the repository at this point in the history
refs: #118
  • Loading branch information
jacobwegner authored Jul 1, 2024
1 parent 6d66c21 commit 0a9522b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modeltrans/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
get_translated_field_label,
)

SUPPORTED_FIELDS = (fields.CharField, fields.TextField)
SUPPORTED_FIELDS = (fields.CharField, fields.TextField, JSONField)

DEFAULT_LANGUAGE = get_default_language()

Expand Down
10 changes: 10 additions & 0 deletions tests/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def __str__(self):
return self.title


class TaggedBlog(models.Model):
title = models.CharField(max_length=255)
tags = JSONField(null=True, blank=True, default=list)

i18n = TranslationField(fields=("title", "tags"))

def __str__(self):
return self.title


class Person(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ChallengeContent,
ChildArticle,
NullableTextModel,
TaggedBlog,
TextModel,
)
from .utils import CreateTestModel
Expand Down Expand Up @@ -119,6 +120,23 @@ def test_fallback_getting_TextField(self):
with override("fr"):
self.assertEqual(m.description_i18n, DESCRIPTION)

def test_fallback_getting_JSONField(self):
m = TaggedBlog.objects.create(title="Falcon", tags=["bird", "raptor"])
with override("de"):
# tags_de is not set, return fallback
self.assertEqual(m.tags_i18n, ["bird", "raptor"])

m = TaggedBlog.objects.create(title="Falcon", tags_fr=[])
with override("fr"):
# tags_fr is set, return the empty list
self.assertEqual(m.tags_i18n, [])

m = TaggedBlog.objects.create(title="Falcon", tags_fr=None)
with override("fr"):
# tags_fr is set to None, return field default (which is
# an empty list)
self.assertEqual(m.tags_i18n, [])

def test_creating_using_virtual_default_language_field(self):
m = Blog.objects.create(title_en="Falcon")

Expand Down
1 change: 1 addition & 0 deletions tests/test_translating.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_get_translated_models(self):
expected = {
app_models.Article,
app_models.Blog,
app_models.TaggedBlog,
app_models.ChildArticle,
app_models.Category,
app_models.Person,
Expand Down

0 comments on commit 0a9522b

Please sign in to comment.