【问题标题】:Using Django-ModelTranslation with Django-Oscar, but "No new translatable fields detected"将 Django-ModelTranslation 与 Django-Oscar 一起使用,但“未检测到新的可翻译字段”
【发布时间】:2018-09-24 02:07:08
【问题描述】:

我正在尝试使用 Django-ModelTranslation (0.12.2) 为 Django-Oscar (1.5.2) 中的产品提供翻译字段。它使用 Django 1.10.8。我关注了Registering Models for Translation 上的文档,但一直收到以下回复:No new translatable fields detected

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

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

project/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 无济于事。我错过了什么?

【问题讨论】:

  • 我认为问题在于没有将您的分叉 oscar 应用程序直接安装在已安装的应用程序下(不确定 oscar 是否允许 / 可以很好地使用它)。不确定如何解决它。
  • @AshishNitinPatil 原来,这是因为 ModelTranslation 会查找 abstract=False 的模型,而 AbstractProduct 有 True 的模型。我把它写在解决方案中。但感谢您与我们联系。

标签: python django mezzanine django-oscar django-modeltranslation


【解决方案1】:

所以,我在 PyCharm 上按了 CTRL+SHIFT+R,在其中输入了No new translatable fields detected 响应并在 ModelTranslation 包中搜索它的来源。我在 modeltranslation.management.commands.sync_translation_fields.Command 中找到了它。这是一个包含句柄()的类,其中包含以下行: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。成功了!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 2018-05-02
    • 2020-11-05
    • 2022-01-23
    • 2019-11-13
    • 2016-04-10
    • 2020-04-16
    • 2017-01-07
    相关资源
    最近更新 更多