question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Translated models can't be used in datamigration

See original GitHub issue

Probably similar to #100

When I try to use translated models in data migration I have an error when I try to create new instance and save it. The error is:

File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 346, in create
    obj = self.model(**kwargs)
File "/path/to/virtualenv/lib/python3.4/site-packages/parler/models.py", line 236, in __init__
    for field in self._parler_meta.get_all_fields():
AttributeError: 'NoneType' object has no attribute 'get_all_fields'

Also when I try to query translated models with specific methods to parler’s TranslatableManager it raises AttributeError.

In the initial migration when model is created in migrations.CreateModel step I see that bases are defined like that:

bases=(parler.models.TranslatableModelMixin, models.Model),

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

17reactions
yanickdicommented, Sep 24, 2019

same issue using django-parler==2.0 django==2.1.7

used python manage.py makemigrations when migrating from my old model:

from django.db import models
from parler.models import TranslatableModel, TranslatedFields

from webapp.core.models.base_model import BaseModel


class MyModel(models.Model):
    class Meta:
        verbose_name = 'My Model'
        verbose_name_plural = 'My Model'

    title = models.CharField(max_length=50)

to new Model:

from django.db import models
from parler.models import TranslatableModel, TranslatedFields

from webapp.core.models.base_model import BaseModel


class MyModel(TranslatableModel):
    class Meta:
        verbose_name = 'My Model'
        verbose_name_plural = 'My Model'

    translations = TranslatedFields(
        title=models.CharField("Title", max_length=50)
    )
Traceback (most recent call last):
  File "manage.py", line 32, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/migration.py", line 114, in apply
    operation.state_forwards(self.app_label, project_state)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 73, in state_forwards
    state.reload_model(app_label, self.model_name_lower, delay=delay)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/state.py", line 158, in reload_model
    self._reload(related_models)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/state.py", line 191, in _reload
    self.apps.render_multiple(states_to_be_rendered)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/state.py", line 306, in render_multiple
    model.render(self)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/state.py", line 574, in render
    return type(self.name, bases, body)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 299, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/state.py", line 337, in register_model
    self.do_pending_operations(model)
  File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 420, in do_pending_operations
    function(model)
  File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 399, in apply_next_model
    self.lazy_model_operation(next_function, *more_models)
  File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 411, in lazy_model_operation
    apply_next_model(model_class)
  File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 399, in apply_next_model
    self.lazy_model_operation(next_function, *more_models)
  File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 385, in lazy_model_operation
    function()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/related.py", line 316, in resolve_related_class
    field.do_related_class(related, model)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/related.py", line 387, in do_related_class
    self.contribute_to_related_class(other, self.remote_field)
  File "/usr/local/lib/python3.7/site-packages/parler/fields.py", line 54, in contribute_to_related_class
    self.model.contribute_translations(cls)
  File "/usr/local/lib/python3.7/site-packages/parler/models.py", line 939, in contribute_translations
    base = shared_model._parler_meta
AttributeError: type object 'MyModel' has no attribute '_parler_meta'

by manually changing the migration File from:

bases=(parler.models.TranslatedFieldsModelMixin, models.Model),

to

bases=(parler.models.TranslatableModel, models.Model),

migrate is doing it’s job.

13reactions
yakkycommented, Nov 11, 2016

I’m working a solution which involves refactoring the base class. Can’t promise any ETA, though, but I’ll keep you updated

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating from django-modeltranslation - Read the Docs
Run ./manage.py migrate to apply the generated data migrations. Your models with translated fields should have a populated i18n field after these migrations....
Read more >
10 Considerations for Data Migrations and Translations
Are you faced with a complex data migration or translation? Do you have years of legacy data that needs to be migrated to...
Read more >
Translating a Data Model - the SEI Online Help
In the Data Models and Views tab, right-click on a Data Model and select Translate Data Model. Example. Note. Languages available for translation...
Read more >
The Difference Between Data Transformation and Data ... - Cleo
Translation can include data validation as well. One example of data translation is to convert EDI purchase order document data into purchase order...
Read more >
Translate a multilingual model
Translating a model creates a secondary model of the language you choose. The secondary model contains the same content as the primary model....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found