我正在尝试使用django-modeltranslation翻译我的模型,而SlugField给我带来了很多麻烦。我已成功注册了要翻译的字段,您可以在db表描述中看到:
Table "public.news_news"
Column | Type | Modifiers
--------------+--------------------------+--------------------------------------------------------
id | integer | not null default nextval('news_news_id_seq'::regclass)
title | character varying(255) | not null
title_en | character varying(255) |
title_nb | character varying(255) |
slug | character varying(50) | not null
slug_en | character varying(50) |
slug_nb | character varying(50) |
但是当我尝试添加新的新闻项时,我收到以下错误:
Traceback: File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
372. return self.admin_site.admin_view(view)(*args, **kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
202. return view(request, *args, **kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
223. return func(*args, **kwargs) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
1036. model_admin=self) File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/contrib/admin/helpers.py" in __init__
35. } for field_name, dependencies in prepopulated_fields.items()] File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/forms/forms.py" in __getitem__
111. raise KeyError('Key %r not found in Form' % name)
Exception Type: KeyError at /en/admin/news/news/add/ Exception Value: u"Key 'slug' not found in Form"
我的admin.py:
from cmsplugin_news.admin import NewsAdmin as OldNewsAdmin
class NewsAdmin(OldNewsAdmin, TranslationAdmin,):
fields = ('title', 'slug')
form = NewNewsForm
models.py:
class News(models.Model):
title = models.CharField(_('Title'), max_length=255)
slug = models.SlugField(
_('Slug'), unique_for_date='pub_date',
)
和forms.py:
class NewNewsForm(forms.ModelForm):
class Meta:
model = News
fields = ('title', 'slug', )
同样,这是我的translation.py,其中我声明了应该由django-modeltranslation翻译的字段。我想无论如何错误不在这里,因为字段已在db:
中翻译class NewsTranslationOptions(TranslationOptions):
fields = ('title', 'slug', 'excerpt', 'content')
translator.register(News, NewsTranslationOptions)
请帮忙吗?我真的被卡住了!谢谢!