This commit is contained in:
Holger Sielaff
2025-08-05 14:44:54 +02:00
parent bc4463b68e
commit 6947b70764
3 changed files with 22 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
Metadata-Version: 2.4 Metadata-Version: 2.4
Name: django-translatable-fields Name: django-translatable-fields
Version: 0.1.2 Version: 0.1.3
Summary: Django plugin that mimics Odoo's translate=True functionality with admin interface integration Summary: Django plugin that mimics Odoo's translate=True functionality with admin interface integration
Author-email: Holger Sielaff <holger@backender.de> Author-email: Holger Sielaff <holger@backender.de>
Maintainer-email: Holger Sielaff <holger@backender.de> Maintainer-email: Holger Sielaff <holger@backender.de>

View File

@@ -162,8 +162,10 @@ class TranslatableMixin:
return json.dumps({current_lang: str(value)}) return json.dumps({current_lang: str(value)})
def validate(self, value, model_instance): def validate(self, value, model_instance):
if not self.translatable or not value or not isinstance(value, (str, dict)): if not self.translatable or not value:
super().validate(value, model_instance) return super().validate(value, model_instance)
if not isinstance(value, dict):
return super().validate(value, model_instance)
# Validate each translation individually # Validate each translation individually
for lang_code, translation in value.items(): for lang_code, translation in value.items():

View File

@@ -0,0 +1,17 @@
from django.db import models
from .fields import TranslatableMixin
from django.core.exceptions import ValidationError
class TranslatableModelMixin(models.Model):
class Meta:
abstract = True
def clean(self):
for field in self._meta.get_fields():
if isinstance(field, TranslatableMixin) and field.translatable:
if not isinstance(getattr(self, field.name), dict):
raise ValidationError(f'Field "{field.name}" must be a dictionary')
if not any(getattr(self, field.name).values()):
raise ValidationError(f'Field "{field.name}" must contain at least one translation')
super().clean()