2

我正在尝试使用 Django-ModelTranslation (0.12.2) 为 Django-Oscar (1.5.2) 中的产品提供翻译字段。它使用 Django 1.10.8。我遵循了关于注册翻译模型的文档,但一直得到这个回应:No new translatable fields detected.

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

下面,我展示了我是如何分叉 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'])

项目/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了也没有用。我错过了什么?

4

1 回答 1

4

因此,我在 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。有效!

现在,无论下一个问题是什么。

于 2018-04-13T19:46:04.587 回答