django-parler: Make existing fields translatable ¶
Procedura per traduzione tabella Product
I campi name, slug e description (pre-esistenti) devono essere tradotti
Commentato ProductAdmin (sostituito con pass)
Rinominati i campi field --> old_field in Product
Creata migrazione "product_rename_old_field"
Product derivato da TranslatableBaseModel anziche' BaseModel
introdotti i campi "translations":
translations = TranslatedFields( name = models.CharField(max_length=1024, verbose_name=_("Name")), slug = models.SlugField(_("Slug"), max_length=255, unique=True), description = models.TextField(blank=True, verbose_name=_("Description")), meta = {'unique_together': [('language_code', 'slug')]}, )
Creata migrazione "product_rename_add_translation_fields"
Fix migrazione precedente:
migrations.CreateModel( name='Product', ... bases=(parler.models.TranslatableModelMixin, models.Model), ),
Creata data migration "product_migrate_translatable_fields":
from django.db import migrations from django.conf import settings def forwards_func(apps, schema_editor): MyModel = apps.get_model('backend', 'Product') MyModelTranslation = apps.get_model('backend', 'ProductTranslation') for object in MyModel.objects.all(): MyModelTranslation.objects.create( master_id=object.pk, language_code=settings.LANGUAGE_CODE, name=object.old_name, slug=object.old_slug, description=object.old_description, ) class Migration(migrations.Migration): dependencies = [ ('backend', '0003_product_add_translation_fields'), ] operations = [ migrations.RunPython(forwards_func), ]
Commentati i campi "old_field" e creata migrazione "product_remove_old_fields"
ProductAdmin derivata da TranslatableBaseModelAdmin anziche' BaseModelAdmin
In ProductAdmin, utilizzato metodo get_prepopulated_fields() anziche' attributo prepopulated_fields
Eseguite migrazioni