def get_form(self, request, obj=None, **kwargs): i18n_field = get_i18n_field(self.model) language = get_language() for field in i18n_field.get_translated_fields(): if field.language == language: field.blank = field.original_field.blank return super().get_form(request, obj=obj, **kwargs)
def test_get_i18n_field(self): self.assertEqual(get_i18n_field(app_models.Blog), app_models.Blog._meta.get_field("i18n")) class I18nFieldTestModel(models.Model): test = models.CharField(max_length=20) class Meta: app_label = "django-modeltrans_tests" self.assertEqual(get_i18n_field(I18nFieldTestModel), None) class I18nFieldTestModel2(models.Model): test = models.CharField(max_length=20) i18n = models.CharField(max_length=20) class Meta: app_label = "django-modeltrans_tests" self.assertEqual(get_i18n_field(I18nFieldTestModel2), None)
def get_edit_handler(self, instance, request): i18n_field = get_i18n_field(type(instance)) tabs = [ObjectList(self.panels, heading='Perustiedot')] if i18n_field: for lang_code, name in settings.LANGUAGES: if lang_code == 'fi': continue fields = [] for field in i18n_field.get_translated_fields(): if field.language != lang_code: continue fields.append(FieldPanel(field.name)) tabs.append(ObjectList(fields, heading=name)) handler = TabbedInterface(tabs) return handler
def get_translations_fieldsets(self): i18n_field = get_i18n_field(self.model) fieldsets = [] for lang_code in settings.MODELTRANS_AVAILABLE_LANGUAGES: if lang_code == settings.LANGUAGE_CODE: continue tab_name = 'general' if lang_code == settings.LANGUAGE_CODE else f'lang-{lang_code}' fieldset = (None, { 'classes': ('suit-tab', f'suit-tab-{tab_name}',), 'fields': [ f'{field.name}' for field in i18n_field.get_translated_fields() if field.name.endswith(lang_code) ] }) fieldsets.append(fieldset) return fieldsets