'NoneType' object has no attribute 'get_all_fields' parler
Jan. 5, 2019, 5:32 a.m.
If you create your own migration to add fields that must be translatable, you get error:
'NoneType' object has no attribute 'get_all_fields'
1 case. Update parler
In newer versions of parler, this error has been fixed, so it’s enough to upgrade the package.
2 case. Add TranslatableModelMixin in base class of model
MyModel.__bases__ = (models.Model, TranslatableModelMixin) after declaration of
For clarity, take an example from the documentation and add the necessary line:
from django.db import migrations from django.conf import settings from django.core.exceptions import ObjectDoesNotExist def forwards_func(apps, schema_editor): MyModel = apps.get_model('myapp', 'MyModel') MyModel.__bases__ = (models.Model, TranslatableModelMixin) MyModelTranslation = apps.get_model('myapp', 'MyModelTranslation') for object in MyModel.objects.all(): MyModelTranslation.objects.create( master_id=object.pk, language_code=settings.LANGUAGE_CODE, name=object.name ) def backwards_func(apps, schema_editor): MyModel = apps.get_model('myapp', 'MyModel') MyModelTranslation = apps.get_model('myapp', 'MyModelTranslation') for object in MyModel.objects.all(): translation = _get_translation(object, MyModelTranslation) object.name = translation.name object.save() # Note this only calls Model.save() def _get_translation(object, MyModelTranslation): translations = MyModelTranslation.objects.filter(master_id=object.pk) try: # Try default translation return translations.get(language_code=settings.LANGUAGE_CODE) except ObjectDoesNotExist: try: # Try default language return translations.get(language_code=settings.PARLER_DEFAULT_LANGUAGE_CODE) except ObjectDoesNotExist: # Maybe the object was translated only in a specific language? # Hope there is a single translation return translations.get() class Migration(migrations.Migration): dependencies = [ ('yourappname', '0001_initial'), ] operations = [ migrations.RunPython(forwards_func, backwards_func), ]
The fact is that the migration requires the
get_all_fields function, which is available in the
TranslatableModelMixin mixin when we inherit our model from
TranslatableModel. But when migrating from the base (inherited) classes, only
models.Model is used, which is why you need to add
You can read more about this problem from the comment on the github: https://github.com/django-parler/django-parler/issues/157#issuecomment-386902848.