KristianOellegaard / django-hvad

Painless translations in django, using the regular ORM. Integrates easily into existing projects and apps. Easy convertible from django-multilingual-ng.
https://github.com/KristianOellegaard/django-hvad
Other
533 stars 127 forks source link

Integrating hvad into an existing app #277

Closed elenaoat closed 8 years ago

elenaoat commented 8 years ago

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?

spectras commented 8 years ago

Hello,

This is a process that requires 4 Django migration steps:

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 :)

spectras commented 8 years ago

(just updated the code sample, there was an obvious mistake in it)

elenaoat commented 8 years ago

Thanks spectras for your quick and clear reply, I will take a look at it. Will let you know if anything isn't clear.

elenaoat commented 8 years ago

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.

spectras commented 8 years ago

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.

elenaoat commented 8 years ago

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.

spectras commented 8 years ago

Closing now, feel free to reopen as needed.

claudep commented 8 years ago

This should be in the docs!