3

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!

Elchinas
  • 139
  • 2
  • 8
  • You say 'after running migrations' but did you try to `makemigration` to see if it generates the new translations fields for your model (and then running them!)? – ppython Feb 15 '18 at 21:51

6 Answers6

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.

Igor Fedun
  • 91
  • 2
  • 4
  • Yeap! That did the trick. I have mistakenly did `pipenv update` which installed `modeltranslation==12` where `==0.13b1` should be used instead. Thanks Igor! – nik_m Feb 16 '19 at 12:58
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?

Yannic Hamann
  • 4,655
  • 32
  • 50
  • Thanks, but I have already tried both ugettext and lazy. Putting modeltranslation at the end doesn't work, since as the docs explain: "If you want to use the admin integration, modeltranslation must be put before django.contrib.admin". It still doesn't work, I don't know what else to try... Have you ever used it? Cause it may have bugs or sth... – Elchinas Feb 15 '18 at 17:50
  • Yes, I am successfully using TranslationAdmin with ``django`` 1.11.5 and ``django-modeltranslation`` 0.12.1. Have you set your ``SITE_ID``? – Yannic Hamann Feb 15 '18 at 21:02
  • The only difference that I can see is that I put ``django.contrib.admin`` at the very end of my ``INSTALLED_APPS `` like so: https://pastebin.com/raw/CdnAdNmy – Yannic Hamann Feb 15 '18 at 21:55
  • At last. I was doing it with django 2.0, and once I switched to 1.11 it immediately worked. Do you think it is not supported? Thank you for your time! – Elchinas Feb 16 '18 at 12:51
  • 1
    Their tests are failing with ``django`` 2.0. https://travis-ci.org/deschler/django-modeltranslation. – Yannic Hamann Feb 16 '18 at 15:12
  • 1
    Sorry for the bump but would be nice if you can accept an answer :) – Yannic Hamann Feb 20 '18 at 09:12
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.

Patrik Beck
  • 2,455
  • 1
  • 19
  • 24
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.

ppython
  • 485
  • 6
  • 19
  • Thanks, but I have tried it but and it returned "No new changes". It still doesn't work, and I don't really know what else to try, I have been days with it... – Elchinas Feb 15 '18 at 17:51
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.

Elchinas
  • 139
  • 2
  • 8
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.

sliawatimena
  • 338
  • 1
  • 4
  • 16
  • This works, however, hardly a good solution to patch downloaded packages. For me, the problem came back right after trying to deploy to heroku. – Patrik Beck May 14 '21 at 09:31