django-parler: Translated models can't be used in datamigration
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),
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 10
- Comments: 18 (8 by maintainers)
Commits related to this issue
- Fix #157 -- Allow translations to be used in data migrations. — committed to knbk/django-parler by knbk 6 years ago
- Fix #157 -- Allow translations to be used in data migrations. — committed to knbk/django-parler by knbk 6 years ago
- Fix #157 -- Allow translations to be used in data migrations. — committed to knbk/django-parler by knbk 6 years ago
- Fix #157 -- Allow translations to be used in data migrations. Adds a TranslationsForeignKey field type that contributes translations to the target model. — committed to knbk/django-parler by knbk 6 years ago
- Fix #157 -- Allow translations to be used in data migrations. Adds a TranslationsForeignKey field type that contributes translations to the target model. — committed to knbk/django-parler by knbk 6 years ago
same issue using django-parler==2.0 django==2.1.7
used
python manage.py makemigrationswhen migrating from my old model:to new Model:
by manually changing the migration File from:
to
migrate is doing it’s job.
I’m working a solution which involves refactoring the base class. Can’t promise any ETA, though, but I’ll keep you updated
Any chance we can get a release that includes this fix?
I experienced the exact same problem. I managed to make it work by setting model bases inside the RunPython script during runtime like this:
MyModel.__bases__ = (models.Model,)Here is the full code:
This way you will not need to modify any migration files.
I was able to make this work by replacing
base = shared_model._parler_metain models.py:939 withbase = getattr(shared_model, '_parler_meta', None)But I’m not sure if this will cause problems in other situations.I’m quite surprised to see
bases=(parler.models.TranslatableModelMixin, models.Model),.This doesn’t activate our metaclass, so
_parler_metawill not be initialized. If you usebases=(parler.models.TranslatableModel,),this issue probably disappears.Which Django version have you used? If you must, you access your translated fields model directly (the only that inherits
TranslatedFieldsModelin the migration).