我是模特翻译的新手,我遇到了问题。在创建模型并在manage.py syncdb
中注册要翻译的字段后执行translation.py
命令时,模型转换应用程序不会将已翻译的字段添加到模型中。但是这些字段在表格中。所以如果我在python shell中创建一个对象,我就无法访问display_en
,因为它会引发错误
AttributeError: 'Content' object has no attribute 'display_en'
我的settings.py:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'test_db', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'admin', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
}
SITE_ID = 1
TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'fr-fr'
ugettext = lambda s: s
LANGUAGES = (
('fr', ugettext('French')),
('en', ugettext('English')),
('ja', ugettext('Japanese')),
)
USE_I18N = True
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
)
USE_L10N = True
USE_TZ = True
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'tagging',
#'social_auth',
'south',
'django.contrib.admin',
'sorl.thumbnail',
'modeltranslation',
'myapp',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
TRANSLATION_REGISTRY = "myapp.translation"
我的models.py:
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
class Test(models.Model):
display = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('test.display'))
url = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('test.url'))
我的translation.py:
from modeltranslation.translator import translator, TranslationOptions
from myapp.models import Test
class TestTranslationOptions(TranslationOptions):
fields = ('display')
translator.register(Test, TestTranslationOptions)
答案 0 :(得分:0)
模型转换是否会加载?使用开发服务器manage.py runserver
将调试信息打印到stdout
。
Validating models...
modeltranslation: Registered 2 models for translation (Foo, Bar) [pid:12345].
0 errors found
[...]
如果你没有看到这个(并且你没有停用DEBUG
),我认为在导入时出现了问题。
您还使用哪个modeltranslation
版本?
自{0.3}以来,TRANSLATION_REGISTRY
设置已弃用,已切换为一致的前缀,因此它变为MODELTRANSLATION_TRANSLATION_REGISTRY
。
在0.4个应用程序级别中,引入了翻译文件,使此设置完全可选。而是现在每个应用程序在其根目录中自动搜索translation.py
。同时添加了MODELTRANSLATION_TRANSLATION_FILES
。它也是可选的,它允许扩展翻译文件列表。
虽然即使在最新的开发版本中也保留对MODELTRANSLATION_TRANSLATION_REGISTRY
的支持以保持向后兼容性,但不再建议使用它。而只需将translation.py
放入要翻译的应用程序的目录中。
最后,fields
中的translation.py
属性应该是元组(或列表),您可以在其中定义字符串。当你添加这样的逗号时,Python只会将它识别为元组:
class TestTranslationOptions(TranslationOptions):
fields = ('display',)
我认为这是你问题中的拼写错误,因为如果找不到字段,我希望modeltranslation
能够摆脱困境。
答案 1 :(得分:0)
您正在使用南方,因此您必须执行./manage.py迁移以翻译您的字段。希望这个帮助