[Django]-Error in db with modeltranslation in django

0👍

It looks like django-modeltranslation doesn’t workwith django 2.0 (at least for me and the installation procedure out there). But it does with django 1.11.

8👍

I had the similar problem with django-modeltranslation.
In Django 2.0.5 I had an error when I opened the admin panel.

Need to update package:

pip install django-modeltranslation==0.13-beta1

update the translations:

python manage.py update_translation_fields

And everything works fine.

1👍

OP is using django-modeltranslation with Django 2.0. But their tests are currently failing for this version.


Use ugettext_lazy in your settings.py to avoid circular imports:

from django.utils.translation import ugettext_lazy as _

LANGUAGES = [
    ('en', _('English')),
    ('th', _('Thai')),
]

MODELTRANSLATION_DEFAULT_LANGUAGE = 'en'
MODELTRANSLATION_LANGUAGES = ('en', 'th')

Try to put your modeltranslation at the end of your INSTALLED_APPS, after the django defaults.

Have you registered your model somewhere else? You can try to unregister it before you register it again.

admin.site.unregister(News)
admin.site.register(News, NewsAdmin)

Are you following the steps with python manage.py makemigration, as stated in the docs?

1👍

It seems "modeltranslations" is no longer being maintained at that particular library name. And later versions of django require on_delete to be set for models.ForeignKey

For me this worked:

pip uninstall modeltranslations
pip install django-modeltranslations

As far as I can tell it’s the same library, but you get a newer version. In my case, I did not need to modify settings.py at all.

0👍

Did you try the sync_translation_fields Management Command?

This command compares the database and translated models definitions (finding new translation fields) and provides SQL statements to alter tables. You should run this command after adding a new language to your settings.LANGUAGES or a new field to the TranslationOptions of a registered model.

0👍

My configuration: Django 2.2.5, Python 3.7.4, bootstrap4-0.1.0

edit ~/anaconda3/envs/django/lib/python3.7/site-packages/modeltranslation/models.py, add on_delete=models.CASCADE

creator_user = models.ForeignKey(User, null=True, default=None, related_name='model_translation', verbose_name=u"User translator", help_text=u"User that created last translation version", on_delete=models.CASCADE,)

edit /anaconda3/envs/django/lib/python3.7/site-packages/modeltranslation/migrations/0001_initial.py, add
import django.db.models.deletion
and on_delete=django.db.models.deletion.CASCADE,

from django.db import models, migrations
import django.db.models.deletion

('creator_user', models.ForeignKey(related_name='model_translation', default=None, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, help_text='Usuario que ha realizado la \xfaltima traducci\xf3n', null=True, verbose_name='Usuario que ha realizado la traducci\xf3n')),

In settings.py
# ModelTranslation
IS_MONOLINGUAL=False
TRANSLATABLE_MODEL_MODULES = ["marketplace.models"]

Modify this with your apps name,
TRANSLATABLE_MODEL_MODULES = [“.models”]

$ python manage.py makemigrations
$ python manage.py migrate

Hope it works for your case too.

Leave a comment