django-modeltranslation
django-modeltranslation copied to clipboard
Polymorphic models
is possible to create an admin class with translation fields in parent django-polymorphic model?
This is an old issue but for anyone coming across this as I did, it's undocumented but it's doable.
My environment:
- Django 3.2
- django-polymorphic 3.0.0
- django-modeltranslation 0.17.3
models.py
class MyModel(PolymorphicModel):
title = models.CharField(verbose_name=_("Title"), max_length=200)
class ChildModel(MyModel):
link = models.URLField(_("link"), blank=True)
translation.py
class MyModelTranslationOptions(TranslationOptions):
fields = ("title",)
class ChildModelTranslationOptions(TranslationOptions):
"""
Leave it blank to translate the title (inherited from MyModel)
or use `fields = ("link",)` if you want to translate the link too
"""
pass
translator.register(MyModel, MyModelTranslationOptions)
translator.register(ChildModel, ChildModelTranslationOptions)
admin.py
@admin.register(ChildModel)
class ChildModelAdmin(TabbedDjangoJqueryTranslationAdmin, PolymorphicChildModelAdmin):
list_display = ["__str__", "link"]
@admin.register(MyModel)
class MyModelAdmin(TabbedDjangoJqueryTranslationAdmin, PolymorphicParentModelAdmin):
list_display = ["__str__"]
base_model = MyModel
child_models = (ChildModel,)
list_filter = (PolymorphicChildModelFilter,)
Hope it helps