Python 使用Django ModelTranslation和Django Oscar,但是;“未检测到新的可翻译字段”;
我试图使用Django ModelTranslation(0.12.2)为Django Oscar(1.5.2)中的产品提供翻译字段。它使用Django 1.10.8。我按照上的文档进行了操作,但不断得到响应:Python 使用Django ModelTranslation和Django Oscar,但是;“未检测到新的可翻译字段”;,python,django,mezzanine,django-oscar,django-modeltranslation,Python,Django,Mezzanine,Django Oscar,Django Modeltranslation,我试图使用Django ModelTranslation(0.12.2)为Django Oscar(1.5.2)中的产品提供翻译字段。它使用Django 1.10.8。我按照上的文档进行了操作,但不断得到响应:未检测到新的可翻译字段 我不知道这是否是问题的一部分,但我首先启动了一个夹层(4.2.3)项目,然后使用Oscar's将Oscar集成到其中。翻译字段被完美地添加到夹层中。(编辑:在一个新的Django项目中添加了带有ModelTranslation的Oscar,响应相同,所以它不是夹层。
未检测到新的可翻译字段
我不知道这是否是问题的一部分,但我首先启动了一个夹层(4.2.3)项目,然后使用Oscar's将Oscar集成到其中。翻译字段被完美地添加到夹层中。(编辑:在一个新的Django项目中添加了带有ModelTranslation的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'])
from modeltranslation.translator import translator, TranslationOptions
from oscar.apps.catalogue.abstract_models import AbstractProduct
class AbstractProductTranslationOptions(TranslationOptions):
fields = ('title', 'description',)
translator.register(AbstractProduct, AbstractProductTranslationOptions)
project/forked\u apps/catalog/translation.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'])
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\u translation\u fields
,但没有结果。我错过了什么?因此,我在PyCharm上按CTRL+SHIFT+R,在其中输入未检测到新的可翻译字段的响应,并搜索ModelTranslation包以查找其来源。我在modeltransation.management.commands.sync\u translation\u fields.Command中找到了它。这是一个包含以下行的句柄()的类:models=translator.get\u registered\u models(abstract=False)
我想,好吧,也许它不起作用,因为oscar.apps.catalog.abstract\u models.AbstractProduct有abstract=True
。我猜死亡赠品是以模块本身的名义
那么,如果它不是抽象产品,那么正确的模型在哪里呢?我决定尝试触发另一个错误来解决它。我在project.catalog.abstract\u models中使用了我自己的产品来替代奥斯卡的abstract\u models.abstract产品:
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\u translation\u字段
。成功了
现在,不管下一个问题是什么。我认为问题在于没有将您的分叉奥斯卡应用程序直接安装在已安装的应用程序下(不确定奥斯卡是否允许/是否能很好地配合)。不确定如何解决它。@ashishnitinpail发现,这是因为ModelTranslation查找abstract=False
的模型,而AbstractProduct具有True
。我把它写在溶液里了。但是谢谢你伸出援手。