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

Using Django-ModelTranslation with Django-Oscar, but "No new translatable fields detected"

我正在尝试使用 Django-ModelT运行slation (0.12.2) 为 Django-Oscar (1.5.2) 中的产品提供 t运行slation 字段。它使用 Django 1.10.8。我遵循了 Registering Models for Translation 上的文档,但不断收到此响应:No new translatable fields detected.

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

下面,我展示了我如何分叉 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无果。我错过了什么?

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

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