Elchinas
Elchinas

Reputation: 139

Error in db with modeltranslation in django

I want to add internationalization to a project, so I use django-modeltranslation app. However, after following all the steps of configuration and running migrations, when I enter in my admin the model is registered, but when I click on it:

"Something's wrong with your database installation. Make sure the appropriate database tables have been created, and make sure the database is readable by the appropriate user."

Here's the code (note I have put it all in a file for clarity):

INSTALLED_APPS = [
    'modeltranslation',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'nuggets',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'Trans',
        'USER': 'postgres',
        'PASSWORD': 'c1l2a3u4',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('es', gettext('Spanish')),
)

#Models code
from django.db import models
class News(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()


#Admin code
from django.contrib import admin
from .models import News
from modeltranslation.admin import TranslationAdmin

class NewsAdmin(TranslationAdmin):
	pass

admin.site.register(News, NewsAdmin)


#translation.py code
from modeltranslation.translator import translator, TranslationOptions
from .models import News

class NewsTranslationOptions(TranslationOptions):
    fields = ('title', 'text',)

translator.register(News, NewsTranslationOptions)

]2]2

I have tried before createing the models, after, with default db, with postgre... Nothing seems to work, help please!

Upvotes: 3

Views: 4531

Answers (6)

Patrik Beck
Patrik Beck

Reputation: 2505

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.

Upvotes: 1

sliawatimena
sliawatimena

Reputation: 338

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.

Upvotes: 0

Igor Fedun
Igor Fedun

Reputation: 111

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.

Upvotes: 8

Yannic Hamann
Yannic Hamann

Reputation: 5225

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?

Upvotes: 1

Elchinas
Elchinas

Reputation: 139

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.

Upvotes: 0

ppython
ppython

Reputation: 493

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.

Upvotes: 0

Related Questions