0

I use a CMS that does a mapping of my models through an API at runtime.

It works fine, except for localized fields, because I use https://django-modeltranslation.readthedocs.io/en/latest/registration.html which add "shadow" fields that are obviously not mapped (they're not present in the model itself, but added through "register")

Is there any way I can tell my model that it owns those fields? It can find the label field, but misses the label_fr and label_en which are dynamically added at runtime.

Here is the 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)

I use https://github.com/jet-admin/jet-django and I noticed that the response of the /model_descriptions endpoint only returns the label field.

I suspect this being the code being called when calling the endpoint https://github.com/jet-admin/jet-django/blob/94b0bb1451e768c7c3b6dadf9830d982914fe6c9/jet_django/views/model_description.py#L12

Basically, I've installed django-modeltranslation and jet-django apps, the later serves an API that is consumed by JET Admin UI and is used to do the models lookup.

I don't know if my issue must be fixed in jet-django itself, or if django provides a feature for shadow fields like that.

Vadorequest
  • 16,593
  • 24
  • 118
  • 215
  • You don't need to show _translation.py_, it's just standard way of specifying the fields to translate. What we need to know, is what's the exact problem: what CMS are you using, how does the mapping happen, what's the code that doesn't return what you'd want. – dirkgroten Mar 07 '19 at 16:08
  • I didn't know that's standard way of doing translations. I thought the way of doing it through `register` was specific to the app. – Vadorequest Mar 07 '19 at 16:09
  • sorry I meant that's how modeltranslation works. But this code isn't your issue, and it's totally unclear what you mean by "the model can find the `label` field but misses the `label_fr` field". What code "finds" a field? What code does a "mapping"? – dirkgroten Mar 07 '19 at 16:11
  • Thanks, I've added additional information – Vadorequest Mar 07 '19 at 16:18
  • I found [this](https://django-modeltranslation.readthedocs.io/en/latest/registration.html#precautions-regarding-registration-approach). When you retrieve an instance of a translated object, (e.g. a 'Post' with a field `text` that is translated), you'll see that `post._meta.fields` contains the translated fields, so `text`, `text_fr`, `text_de`. But the issue is probably that the jet API code is executed before the modeltranslation registration. Not sure how to solve that, you may want to ask the guys from jet-admin. – dirkgroten Mar 08 '19 at 11:51
  • 1
    The fields are retrieved [here](https://github.com/jet-admin/jet-django/blob/94b0bb1451e768c7c3b6dadf9830d982914fe6c9/jet_django/admin/model_description.py#L44) using `Model._meta.get_fields()`. So it should work if the translation registration runs before the JetAdmin registration. Could it be that changing the order of the apps in your settings.py would do the job? – dirkgroten Mar 08 '19 at 11:59
  • Damn, totally could, hadn't thought about that! I'll give it a try, thanks :) – Vadorequest Mar 11 '19 at 12:36
  • @dirkgroten Indeed it worked. If you want to write an actual answer I'll accept it! :) – Vadorequest Mar 11 '19 at 20:16

1 Answers1

1

django-modeltranslation uses a registration approach, described here, which means all models are patched when you launch your django app the first time. After django-modeltranslation is initialised, Post._meta.fields contains the translated fields text_fr and text_de in addition to text.

Looking at jet-django, it seems a JetAdminModelDescription is also initialised when the app is launched, the actual model fields are retrieved here using Model._meta.get_fields().

So as long as jet-django is initialised after django-modeltranslations, the fields should be also available to JetAdmin.

Make sure you place jet-django after django-modeltranslation in your INSTALLED_APPS setting and it should work.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42