出于某种原因,我在添加到管理员时没有看到任何语言选项卡。我正在使用 Django 1.9.10。我正在使用 django-hvad 但决定尝试 parler。我在一个新项目中尝试了相同的确切代码并且它有效,但在我现有的项目中它没有。请注意,我也在使用 django-mptt。在与 mptt 模型不同的模型中尝试 parler,反之亦然。
settings/base.py
# Translations
gettext = lambda s: s
LANGUAGES = (
('en', gettext('English')),
('ar', gettext('Arabic')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, "locale"),
)
# # Parler Configuration
PARLER_LANGUAGES = {
None: (
{'code': 'ar', },
{'code': 'en',},
),
'default': {
'fallback': 'ar', # defaults to PARLER_DEFAULT_LANGUAGE_CODE
'hide_untranslated': False, # the default; let .active_translations() return fallbacks too.
}
}
PARLER_DEFAULT_LANGUAGE_CODE = 'ar'
模型
class Category(MPTTModel, TranslatableModel):
slug = models.SlugField(max_length=50, unique=True, null=True, blank=True)
translations = TranslatedFields(
title = models.CharField(max_length=90, unique=True, null=True, blank=True)
)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
objects = CategoryManager()
def __unicode__(self):
return unicode(self.slug) or u''
def get_absolute_url(self):
return reverse("category:detail", kwargs={"slug": self.slug})
class Meta:
ordering = ["slug"]
verbose_name = _("Category")
verbose_name_plural = _("Categories")
管理员.py
class CategoryAdminForm(MPTTAdminForm, TranslatableModelForm):
pass
class CategoryAdmin(TranslatableAdmin, MPTTModelAdmin):
form = CategoryAdminForm
def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('title',)} # needed for translated fields
admin.site.register(Category, CategoryAdmin)