From 7d92e3982f8c4c38d9c7223d5fd3a05774ed8682 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 22 Oct 2020 16:32:03 +0100 Subject: [PATCH] Fix steps for adding translations to an existin snippet * Fix BootstrapTranslatableMixin to skip TranslatableModel's system check for the missing unique constraint * Fix incorrect import for BootstrapTranslatableModel and use of `--empty` in final schema migration * Clarify handling of Meta classes, the fact that you need to run makemigrations for step 1, and what to do for the non-nullable 'locale' prompt --- docs/advanced_topics/i18n.rst | 19 +++++++++++++++++-- wagtail/core/models.py | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/advanced_topics/i18n.rst b/docs/advanced_topics/i18n.rst index a6fb26ff38..ceea31ea42 100644 --- a/docs/advanced_topics/i18n.rst +++ b/docs/advanced_topics/i18n.rst @@ -508,6 +508,13 @@ This will add the two fields without any constraints: class Advert(BootstrapTranslatableMixin, models.Model): name = models.CharField(max_length=255) + # if the model has a Meta class, ensure it inherits from + # BootstrapTranslatableMixin.Meta too + class Meta(BootstrapTranslatableMixin.Meta): + verbose_name = 'adverts' + +Run ``python manage.py makemigrations myapp`` to generate the schema migration. + Step 2: Create a data migration ******************************* @@ -524,7 +531,7 @@ in that app: .. code-block:: python from django.db import migrations - from wagtail_localize.bootstrap import BootstrapTranslatableModel + from wagtail.core.models import BootstrapTranslatableModel class Migration(migrations.Migration): dependencies = [ @@ -556,6 +563,9 @@ constraints: class Advert(TranslatableMixin, models.Model): # Change this line name = models.CharField(max_length=255) + class Meta(TranslatableMixin.Meta): # Change this line, if present + verbose_name = 'adverts' + Step 4: Run ``makemigrations`` to generate schema migrations, then migrate! *************************************************************************** @@ -565,9 +575,14 @@ migrations: .. code-block:: bash - python manage.py makemigrations myapp --empty + python manage.py makemigrations myapp python manage.py migrate +When prompted to select a fix for the nullable field 'locale' being changed to +non-nullable, select the option "Ignore for now" (as this has been handled by the +data migration). + + Translation workflow -------------------- diff --git a/wagtail/core/models.py b/wagtail/core/models.py index 9996cdf3ab..d0ad2d34f8 100644 --- a/wagtail/core/models.py +++ b/wagtail/core/models.py @@ -574,6 +574,11 @@ class BootstrapTranslatableMixin(TranslatableMixin): Locale, on_delete=models.PROTECT, null=True, related_name="+", editable=False ) + @classmethod + def check(cls, **kwargs): + # skip the check in TranslatableMixin that enforces the unique-together constraint + return super(TranslatableMixin, cls).check(**kwargs) + class Meta: abstract = True