Files
Django-Translatable-Fields/django_translatable_fields/models.py
Holger Sielaff 6947b70764 ...
2025-08-05 14:44:54 +02:00

18 lines
697 B
Python

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()