我正在使用django-hvad在Django中使用多语言应用程序。
我有两个模型,分类和博客应用的帖子:
class Category(TranslatableModel):
translations = TranslatedFields(
name=models.CharField(
max_length=100,
verbose_name=_(u'Nombre')
)
)
slug = AutoSlugField(
populate_from='name'
)
class Meta:
verbose_name = _(u'Categoría')
verbose_name_plural = _(u'Categorías')
def __unicode__(self):
return self.safe_translation_getter('name', str(self.pk))
class Post(TranslatableModel):
category = models.ForeignKey(
Category,
related_name='posts',
verbose_name=_(u'Categoría')
)
slug = AutoSlugField(
populate_from='title'
)
translations = TranslatedFields(
title=models.CharField(
max_length=100,
verbose_name=_(u'Título')
),
content=RichTextField(
verbose_name=_(u'Contenido')
)
)
class Meta:
verbose_name = _(u'Publicación')
verbose_name_plural = _(u'Publicaciones')
ordering = ['-created']
def __unicode__(self):
return self.safe_translation_getter('title', str(self.pk))
在我的admin.py中,我以这种方式注册我的模型:
from django.contrib import admin
from hvad.admin import TranslatableAdmin
from blog.models import Category, Post
class CategoryAdmin(TranslatableAdmin):
pass
admin.site.register(Category, CategoryAdmin)
class PostAdmin(TranslatableAdmin):
pass
admin.site.register(Post, PostAdmin)
当我在管理界面中创建新帖子时,我在类别下拉列表中有类别ID而不是他们的名字:
如何获取翻译后的名称而非ID?