1

我正在尝试使用 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

模型.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 翻译的字段。我想无论如何错误不在这里,因为这些字段已经在数据库中翻译:

class NewsTranslationOptions(TranslationOptions):
    fields = ('title', 'slug', 'excerpt', 'content')

translator.register(News, NewsTranslationOptions)

请问有什么帮助吗?我真的被困住了!谢谢!

4

0 回答 0