Files

26 lines
847 B
Python
Raw Permalink Normal View History

2025-08-02 20:08:33 +02:00
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