0

我使用一个 CMS,它在运行时通过 API 对我的模型进行映射。

它工作正常,除了本地化字段,因为我使用https://django-modeltranslation.readthedocs.io/en/latest/registration.html添加明显未映射的“影子”字段(它们不存在于模型本身,但通过“注册”添加)

有什么方法可以告诉我的模型它拥有这些字段?它可以找到该label字段,但错过了在运行时动态添加的label_fr和。label_en

这是translation.py

from modeltranslation.translator import translator, TranslationOptions

from tfp_backoffice.apps.org.models import Org


class OrgTranslationOptions(TranslationOptions):
  """
  See https://django-modeltranslation.readthedocs.io/en/latest/registration.html
  """
  fields = ('label',)
  required_languages = ('fr',)  # ex: {'de': ('title', 'text'), 'default': ('title',)}


translator.register(Org, OrgTranslationOptions)

我使用https://github.com/jet-admin/jet-django,我注意到/model_descriptions端点的响应只返回该label字段。

我怀疑这是调用端点时调用的代码https://github.com/jet-admin/jet-django/blob/94b0bb1451e768c7c3b6dadf9830d982914fe6c9/jet_django/views/model_description.py#L12

基本上,我已经安装了django-modeltranslation应用jet-django程序,后者提供了一个由JET Admin UI使用并用于进行模型查找的 API。

我不知道我的问题是否必须在 jet-django 本身中解决,或者 django 是否为类似的影子字段提供了一个功能。

4

1 回答 1

1

django-modeltranslation 使用注册方法,描述在这里,这意味着当你第一次启动你的 django 应用程序时,所有模型都会被修补。初始化 django-modeltranslation 后,Post._meta.fields包含已翻译的字段text_fr以及text_de.text

查看 jet-django,似乎 aJetAdminModelDescription在应用程序启动时也被初始化,实际的模型字段在这里使用Model._meta.get_fields().

因此,只要在 django-modeltranslations 之后初始化 jet-django,JetAdmin 也应该可以使用这些字段。

确保在设置中的 django-modeltranslation之后放置 jet-django INSTALLED_APPS,它应该可以工作。

于 2019-03-12T14:38:49.480 回答