wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
222 stars 84 forks source link

TranslatableModelAdmin - does it work ? #698

Closed kiyoshi-satoo closed 1 year ago

kiyoshi-satoo commented 1 year ago
class FontLicence(ClusterableModel):
    name = models.CharField(max_length=255, verbose_name="Licence", unique=True)

    def __str__(self):
        return f'{self.name}'

class FontLicenceAdmin(TranslatableModelAdmin):
    model = FontLicence
    menu_order = 600

modeladmin_register(FontLicenceAdmin)
django.core.exceptions.ImproperlyConfigured: Model `<class 'apps.font.models.FontLicence'>` used in translatable admin `<class 'apps.core.wagtail_hooks.FontLicenceAdmin'>` must subclass the `<class 'wagtail.models.i18n.TranslatableMixin'>` class.
zerolab commented 1 year ago

The error is clear in the fact that your model (FontLicence) must inherit from TranslatableMixin. The wagtail-localize docs (and the Wagtail docs) also ask that your custom model inherit from TranslatableMixin in order for the translation mechanisms to work

kiyoshi-satoo commented 1 year ago

Thanks for the answer @zerolab . I have changed the code. But now I am getting error in migrate.I noticed that this happens when applying migrations to existing models.Are there any solutions for applying migrations on existing data ?

class FontLicence(TranslatableMixin,ClusterableModel):
    name = models.CharField(max_length=255, verbose_name="Licence", unique=True)

    class Meta:
        unique_together = ('translation_key', 'locale')

    def __str__(self):
        return f'{self.name}'
UNIQUE constraint failed: font_fontlicence.translation_key, font_fontlicence.locale_id
zerolab commented 1 year ago

This happens because you already have content. See https://docs.wagtail.org/en/stable/advanced_topics/i18n.html#making-snippets-with-existing-data-translatable for steps on how to adress this.

kiyoshi-satoo commented 1 year ago

@zerolab Thanks, it's working. Didn't notice it in the documentation :)