Skip to content

Commit

Permalink
fix: Add support for JSONField
Browse files Browse the repository at this point in the history
Merge pull request #685 from Hopiu/master
  • Loading branch information
last-partizan committed Jun 2, 2023
2 parents d315a90 + b3debe8 commit 25f7305
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions modeltranslation/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Above implies also CommaSeparatedIntegerField, EmailField, FilePathField, SlugField
# and URLField as they are subclasses of CharField.
fields.TextField,
fields.json.JSONField,
fields.IntegerField,
# Above implies also BigIntegerField, SmallIntegerField, PositiveIntegerField and
# PositiveSmallIntegerField, as they are subclasses of IntegerField.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def handle(self, *args, **options):
def_lang_fieldname = build_localized_fieldname(field_name, lang)

# We'll only update fields which do not have an existing value
q = Q(**{def_lang_fieldname: None})
q = Q(**{f"{def_lang_fieldname}__isnull": True})
field = model._meta.get_field(field_name)
if isinstance(field, ManyToManyField):
trans_field = getattr(model, def_lang_fieldname)
Expand Down
3 changes: 3 additions & 0 deletions modeltranslation/tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ class Migration(migrations.Migration):
('genericip', models.GenericIPAddressField(blank=True, null=True)),
('genericip_de', models.GenericIPAddressField(blank=True, null=True)),
('genericip_en', models.GenericIPAddressField(blank=True, null=True)),
('json', models.JSONField(blank=True, null=True)),
('json_de', models.JSONField(blank=True, null=True)),
('json_en', models.JSONField(blank=True, null=True)),
],
),
migrations.CreateModel(
Expand Down
1 change: 1 addition & 0 deletions modeltranslation/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class OtherFieldsModel(models.Model):
datetime = models.DateTimeField(blank=True, null=True)
time = models.TimeField(blank=True, null=True)
genericip = models.GenericIPAddressField(blank=True, null=True)
json = models.JSONField(blank=True, null=True)


class FancyDescriptor:
Expand Down
29 changes: 28 additions & 1 deletion modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ def test_update_or_create_existing(self):

def test_update_or_create_new(self):
instance, created = models.TestModel.objects.update_or_create(
pk=1, defaults={'title_de': 'old de', 'title_en': 'old en'},
pk=1,
defaults={'title_de': 'old de', 'title_en': 'old en'},
)

assert created is True
Expand Down Expand Up @@ -1473,6 +1474,9 @@ def test_translated_models(self):
assert 'decimal' in field_names
assert 'decimal_de' in field_names
assert 'decimal_en' in field_names
assert 'json' in field_names
assert 'json_de' in field_names
assert 'json_en' in field_names
inst.delete()

def test_translated_models_integer_instance(self):
Expand Down Expand Up @@ -2214,6 +2218,29 @@ def test_update_command_invalid_language_param(self):
with pytest.raises(CommandError):
call_command('update_translation_fields', language='xx', verbosity=0)

def test_update_command_with_json_field(self):
"""
Test that the update_translation_fields command works with JSON fields.
"""
instance_pk = models.OtherFieldsModel.objects.create(json={'foo': 'bar'}).pk
models.OtherFieldsModel.objects.all().rewrite(False).update(json_de=None)

instance = models.OtherFieldsModel.objects.filter(pk=instance_pk).raw_values()[0]

assert instance['json'] == {'foo': 'bar'}
assert instance['json_de'] is None
assert instance['json_en'] is None

call_command(
'update_translation_fields', 'tests', model_name='OtherFieldsModel', verbosity=0
)

instance = models.OtherFieldsModel.objects.filter(pk=instance_pk).raw_values()[0]

assert instance['json'] == {'foo': 'bar'}
assert instance['json_de'] == {'foo': 'bar'}
assert instance['json_en'] is None


class TranslationAdminTest(ModeltranslationTestBase):
def setUp(self):
Expand Down
1 change: 1 addition & 0 deletions modeltranslation/tests/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class OtherFieldsModelTranslationOptions(TranslationOptions):
'date',
'datetime',
'time',
'json',
)


Expand Down

0 comments on commit 25f7305

Please sign in to comment.