Skip to main content
 首页 » 编程设计

python之将 Django-ModelTranslation 与 Django-Oscar 一起使用,但 "No new translatable fields detected"

2024年08月16日31bhlsheji

我正在尝试使用 Django-ModelTranslation (0.12.2) 为 Django-Oscar (1.5.2) 中的产品提供翻译字段。它使用 Django 1.10.8。我遵循了 Registering Models for Translation 上的文档但不断收到此响应:未检测到新的可翻译字段。

我不知道这是否是问题的一部分,但我首先启动了夹层 (4.2.3) 项目,然后使用 Oscar 的 docs 将 Oscar 集成到其中。 . Mezzanine 完美添加了翻译字段。 (编辑:将带有 ModelTranslation 的 Oscar 添加到新的 Django 项目中,响应相同,因此它不是夹层。)

下面,我展示了我如何 fork Oscar 的目录应用并将其添加到 settings.py。

项目/settings.py:

from oscar import get_core_apps 
 
 
# Django-ModelTranslation settings 
 
USE_MODELTRANSLATION = True 
MODELTRANSLATION_FALLBACK_LANGUAGES = ('en',) 
 
LANGUAGES = ( 
    ('en', _('English')), 
    ('de', _('German')), 
) 
 
 
INSTALLED_APPS = [ 
    ..., 
] + get_core_apps(['forked_apps.catalogue']) 

project/forked_apps/catalogue/translation.py:

from modeltranslation.translator import translator, TranslationOptions 
from oscar.apps.catalogue.abstract_models import AbstractProduct 
 
class AbstractProductTranslationOptions(TranslationOptions): 
    fields = ('title', 'description',) 
 
translator.register(AbstractProduct, AbstractProductTranslationOptions) 

然后我运行了 sync_translation_fields 但没有成功。我错过了什么?

请您参考如下方法:

因此,我在 PyCharm 上按了 CTRL+SHIFT+R,输入了 No new translatable fields detected 响应并在 ModelTranslation 包中搜索它的来源。我在 modeltranslation.management.commands.sync_translation_fields.Command 中找到了它。这是一个包含具有以下行的 handle() 的类:models = translator.get_registered_models(abstract=False)。

我想,好吧,也许它不起作用,因为 oscar.apps.catalogue.abstract_models.AbstractProduct 有 abstract=True。我想死的赠品是模块本身的名称。

那么,如果不是 AbstractProduct,那么正确的模型在哪里?我决定尝试触发另一个错误来弄清楚。我在 project.catalogue.abstract_models 中用我自己的覆盖了 Oscar 的 abstract_models.AbstractProduct:

from django.db import models 
from django.utils.translation import get_language, pgettext_lazy 
from django.utils.translation import ugettext_lazy as _ 
 
from oscar.apps.catalogue.abstract_models import AbstractProduct 
 
class AbstractProduct(AbstractProduct): 
    title = models.CharField(pgettext_lazy(u'Product title', u'Title'), 
                             max_length=255, blank=True) 
    description = models.TextField(_('Description'), blank=True) 
 
from oscar.apps.catalogue.models import * 

它产生了这个错误:

ERRORS: 
catalogue.AbstractProduct.product_class: (fields.E304) Reverse accessor for 'AbstractProduct.product_class' clashes with reverse accessor for 'Product.product_class'. 
... 

它持续了 20 多行,但第一行就足够了。正确的模型是产品。于是,我再次去寻找,在 oscar.apps.catalogue.models.Product 中找到了它。毫不奇怪,它看起来像这样:Product(AbstractProduct)。

只剩下两件事要做。首先,我在 project.forked_apps.catalogue 中编辑了我的 translation.py:

from modeltranslation.translator import register, translator, TranslationOptions 
# DELETED: from oscar.apps.catalogue.abstract_models import AbstractProduct 
from oscar.apps.catalogue.models import Product 
 
# Updated the following accordingly. 
class ProductTranslationOptions(TranslationOptions): 
    fields = ('title', 'description',) 
 
translator.register(Product, ProductTranslationOptions) 

其次,我运行了 python manage.py sync_translation_fields。成功了!

现在,不管下一个问题是什么。