You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello!
It was discussed in another issue, but I cannot find it. So I'll try to summarize it from memory. Let's say you have this model:
classMyModel(models.Model):
foo_field=models.CharField() # this one will not be translatablebar_field=models.CharField() # this one will be translatable
The best way to handle an initial migration to hvad is like this:
In the to-be-made-translatable models, rename all translatable fields:
Create a Django migration for this step. The automatic migration will not work (it will delete the field and recreate it, loosing all data). You must create an empty migration like this:
python manage.py makemigrations --empty myApp
Then, in the generated migration's operations, add the following line for each field to rename:
Create another migration. Django will build it correctly, so let it do its job:
python manage.py makemigrations myApp
Apply the migration, and move onto the next step.
Now, we must copy over the data from the temporary fields into the new fields. You must decide the language they will be assigned to. Let's say English. Create yet another migration, empty again. Write a small function at the top, to copy over data. Note that hvad features are not available in migrations, so we must explicitly address the translations' model. For instance:
It is usually a good practice to make migrations reversible. For this, we will need another function that will copy back English fields to the temporary fields:
Create the last migration. Django will build it correctly too, so let it do its job:
python manage.py makemigrations myApp
Apply the migration, and you're done!
That should do it. Let me know if you run into any issue, and as always try running the migrations on you dev environment first, both forwards and backwards.
Also, remember that even though the migration will be fully reversible, applying it backward will delete all translations that are not in the language you chose as a default language.
The text was updated successfully, but these errors were encountered:
Yep, definitely needs doc. Migrating from untranslated models is a common use case, and yet there is no mention of that in the doc.
I had to assume there was no particular procedure and that I could translate afterwards, hit the wall, curse hvad for not working the way I expected, to eventually find this issue. I had a hunch something like that would happen, but since the doc says nothing about migrating untranslated model...
Hello!
It was discussed in another issue, but I cannot find it. So I'll try to summarize it from memory. Let's say you have this model:
The best way to handle an initial migration to hvad is like this:
In the to-be-made-translatable models, rename all translatable fields:
Create a Django migration for this step. The automatic migration will not work (it will delete the field and recreate it, loosing all data). You must create an empty migration like this:
Then, in the generated migration's operations, add the following line for each field to rename:
Apply the migration, and move onto the next step.
Then, create the translatable fields themselves, alongside the renamed fields. Don't forget to make the model inherit from
TranslatableModel
now:Create another migration. Django will build it correctly, so let it do its job:
Apply the migration, and move onto the next step.
Now, we must copy over the data from the temporary fields into the new fields. You must decide the language they will be assigned to. Let's say English. Create yet another migration, empty again. Write a small function at the top, to copy over data. Note that hvad features are not available in migrations, so we must explicitly address the translations' model. For instance:
It is usually a good practice to make migrations reversible. For this, we will need another function that will copy back English fields to the temporary fields:
Now, just glue together the whole thing by adding those functions as a migration step:
Apply the migration, and move onto the next step.
We're almost done. All we need now is to remove the temporary field. Simply do that:
Create the last migration. Django will build it correctly too, so let it do its job:
Apply the migration, and you're done!
That should do it. Let me know if you run into any issue, and as always try running the migrations on you dev environment first, both forwards and backwards.
Also, remember that even though the migration will be fully reversible, applying it backward will delete all translations that are not in the language you chose as a default language.
The text was updated successfully, but these errors were encountered: