from django.contrib import admin from django.forms import fields from .fields import CharField, TextField from .widgets import TranslatableTextWidget, TranslatableTextareaWidget class TranslatableAdminMixin: """ Mixin for ModelAdmin classes to automatically use translatable widgets for translatable fields. """ def formfield_for_dbfield(self, db_field, request, **kwargs): # Let the fields handle their own form field creation if isinstance(db_field, (CharField, TextField)) and db_field.translatable: return db_field.formfield(**kwargs) return super().formfield_for_dbfield(db_field, request, **kwargs) class TranslatableModelAdmin(TranslatableAdminMixin, admin.ModelAdmin): """ ModelAdmin that automatically handles translatable fields. """ pass