2013-01-16 20 views
0

我是新的模型翻譯,並且出現問題。在創建我的模型並註冊要在translation.py中翻譯的字段後,當我執行manage.py syncdb命令時,模型翻譯應用程序不會將翻譯的字段添加到模型中。字段在表格中。所以,如果我創建在Python殼的對象我無法訪問display_en,因爲它會引發錯誤modeltranslation應用程序不會在模型中添加翻譯的字段

AttributeError: 'Content' object has no attribute 'display_en' 

我的settings.py:

DEBUG = True 
    TEMPLATE_DEBUG = DEBUG 

    ADMINS = (
     # ('Your Name', '[email protected]'), 
    ) 

    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')) 

我的翻譯.py:

from modeltranslation.translator import translator, TranslationOptions 

    from myapp.models import Test 


    class TestTranslationOptions(TranslationOptions): 
     fields = ('display') 

    translator.register(Test, TestTranslationOptions) 

回答

0

模型翻譯加載嗎?使用開發服務器manage.py runserver將調試信息打印到stdout

Validating models... 

modeltranslation: Registered 2 models for translation (Foo, Bar) [pid:12345]. 
0 errors found 
[...] 

如果你沒有看到這一點(和你沒有禁用DEBUG)我想不順心的事在導入時。

另外還有哪個版本的modeltranslation

TRANSLATION_REGISTRY設置從0.3開始被棄用,後者切換爲一致的前綴,因此它變爲MODELTRANSLATION_TRANSLATION_REGISTRY

在0.4個應用程序級別的翻譯文件中,引入了該設置完全可選。現在,每個應用程序現在都會在其根目錄中自動搜索translation.py。同時增加了MODELTRANSLATION_TRANSLATION_FILES。它也是可選的,它允許擴展翻譯文件的列表。

即使在最新的開發版本中保留對MODELTRANSLATION_TRANSLATION_REGISTRY的支持以保持向後兼容性,但不再推薦使用它。相反,只需將translation.py放入您要翻譯的應用程序的目錄即可。

最後,translation.py中的fields屬性應該是一個元組(或列表),您在此定義一個字符串。

class TestTranslationOptions(TranslationOptions): 
    fields = ('display',) 

我想這是你的問題一個錯字,因爲我希望modeltranslation保釋出來,如果它不能找到字段:當您添加一個逗號這樣Python將只承認它作爲一個元組。

0

您正在使用南方,所以您必須執行./manage.py遷移來翻譯您的字段。希望得到這個幫助