Closed elenaoat closed 8 years ago
Hello,
This is a process that requires 4 Django migration steps:
_temp
suffix.There is a gotcha though: core hvad features are not available inside a migration. This means you have to directly use the translation model. Here is a sample data migration function for an hypothetical Book model:
from django.db import migrations, models
def add_translations(apps, schema_editor):
Book = apps.get_model("my_bookapp", "Book")
BookTranslation = Book._meta.translations_model
# if previous line does not work, comment it out and try following line instead:
# BookTranslation = apps.get_model("my_bookapp", "BookTranslation")
for book in Book.objects.all():
BookTranslation.objects.create(
master=book,
language_code='en',
title_temp=book.title,
description_temp=book.description
)
class Migration(migrations.Migration):
initial = True
dependencies = [
('my_bookapp', 'last_migration_name_here'),
]
operations = [
migrations.RunPython(combine_names, migrations.RunPython.noop),
]
Do not copy-paste the whole file, use the Django command python manage.py makemigrations --empty yourappname
to create the skeleton and fill in the blanks.
I did not test it, so it may require some tweaking, but that's the idea. Please feel free to tell if you encounter any issue in the process :)
(just updated the code sample, there was an obvious mistake in it)
Thanks spectras for your quick and clear reply, I will take a look at it. Will let you know if anything isn't clear.
I just tried the above, but unfortunately we are still using Django 1.6 and there is nothing about data migrations for this version of Django in documentation (or am I wrong?). I decided that I would instead create those temporary fields and just write a script to copy data from untranslated fields to translated ones.
Before Django 1.7, this feature was provided by a third party package, django-south
. Its syntax was a bit different though and unless you already use it, there would be no point starting now.
A custom script would work too, or even directly altering the tables in the database as well. Depends on what you are most comfortable with.
Yes, in the end I have just altered the tables directly in the shell as it seemed like the quickest and easiest way to get things done.
Closing now, feel free to reopen as needed.
This should be in the docs!
If I "plug in" into my application and modify an already existing field (i.e., make it translatable), and I make migrations, the data is lost. This is obviously happening because the field is being deleted and a new table is created. How could one integrate hvad into an already existing app, without losing the data for the fields that become translatable?