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



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

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

下面,我将展示如何分叉奥斯卡的目录应用程序并将其添加到 settings.py。

项目/设置.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/目录/翻译.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找到了它。命令。这是一个包含句柄()的类,该行为:models = translator.get_registered_models(abstract=False)

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

那么,如果它不是AbstractProduct,那么正确的模型在哪里?我决定尝试触发另一个错误来弄清楚。我推翻了奥斯卡的abstract_models。抽象产品与我自己的project.catalogue.abstract_models:

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.catalog.models.Product中找到了它。毫不奇怪,它看起来像这样:Product(AbstractProduct).

只剩下两件事要做。首先,我在project.forked_apps.catalog中编辑了我的 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.成功了!

现在,继续下一个问题是什么。

相关内容

最新更新