Home > Enterprise >  How to display/see the default values in Admin for translated fields using Django's modeltransl
How to display/see the default values in Admin for translated fields using Django's modeltransl

Time:11-25

I'm trying to translate some models in an existing project, and I'm learning how to do that using the enter image description here

But the original name field and its value are not shown. Even in the model's table now there are no names displayed at all as can be seen here:

enter image description here

Questions:

  1. How can I see the original value of these translated fields?
  2. How do I keep seeing Equipment names in the overall model's view (the second screenshot)?

CodePudding user response:

"Django Modeltranslate" creates new database fields, in your case name_en and name_es without removing the original name field.

You'will need to copy the contents of the old name field to the generated ones, and I think that the easy way is to write a custom migration. You can create a file in the migrations folder of your app, with content similar to, and run the migrations command:

def _copy_initial_names(apps, schema_editor):
    from your_app.models import Equipment
    for e in Equipment.objects.all():
        e.name_es = e.name
        e.name_en = e.name
        e.save()

class Migration(migrations.Migration):

    dependencies = [
        ('your-app', '00xx_migration_when_modeltranslation_installs'),
    ]

    operations = [
        migrations.RunPython(_copy_initial_names)
    ]

You can do it runing an sql query against your database or otherwise, but with a migration you don't need to remember to do it in another enviroment or in the future.

  • Related