From 6947b7076429c3a3b8f072268e86b07fd2fe3f29 Mon Sep 17 00:00:00 2001 From: Holger Sielaff Date: Tue, 5 Aug 2025 14:44:54 +0200 Subject: [PATCH] ... --- django_translatable_fields.egg-info/PKG-INFO | 2 +- django_translatable_fields/fields.py | 6 ++++-- django_translatable_fields/models.py | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 django_translatable_fields/models.py diff --git a/django_translatable_fields.egg-info/PKG-INFO b/django_translatable_fields.egg-info/PKG-INFO index 172b229..948ff6b 100644 --- a/django_translatable_fields.egg-info/PKG-INFO +++ b/django_translatable_fields.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.4 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 Author-email: Holger Sielaff Maintainer-email: Holger Sielaff diff --git a/django_translatable_fields/fields.py b/django_translatable_fields/fields.py index 3f92d6c..3eab78c 100644 --- a/django_translatable_fields/fields.py +++ b/django_translatable_fields/fields.py @@ -162,8 +162,10 @@ class TranslatableMixin: return json.dumps({current_lang: str(value)}) def validate(self, value, model_instance): - if not self.translatable or not value or not isinstance(value, (str, dict)): - super().validate(value, model_instance) + if not self.translatable or not value: + return super().validate(value, model_instance) + if not isinstance(value, dict): + return super().validate(value, model_instance) # Validate each translation individually for lang_code, translation in value.items(): diff --git a/django_translatable_fields/models.py b/django_translatable_fields/models.py new file mode 100644 index 0000000..58e3172 --- /dev/null +++ b/django_translatable_fields/models.py @@ -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()