theatlantic / django-nested-admin

Django admin classes that allow for nested inlines
http://django-nested-admin.readthedocs.org/
Other
721 stars 99 forks source link

Tabular Inline with Parler #181

Open JPPFaria opened 4 years ago

JPPFaria commented 4 years ago

Hello,

I'm having issues using both nested admin and django parler for a tabular inline.

An example of what I'm doing: class MyModelInline(NestedTabularInline, TranslatableTabularInline): ....

The interface shows everything just fine, but when I select a different language and fill the translatable fields, the text is saved in the default language of parler, instead of the selected one. The translations in the base model work fine, but the ones inside the inline are the ones that give issues.

I believe parler defines the language in a GET parameter (&language=en)

Has anyone managed to get it working, or have any hint on how to do it?

venv: Django==2.2.12 django-nested-admin==3.3.2 django-parler==2.0.1

Python 3.7.7

Thanks, João Faria

UtkucanBykl commented 3 years ago

I had a similar issue. I handle it with override Nested StackedInline.

from parler.utils.views import get_language_parameter
from nested_admin.nested import NestedStackedInline

class TranslatableNestedStackedInline(NestedStackedInline):
    def get_formset(self, request, obj=None, **kwargs):
        FormSet = BaseFormSet = kwargs.pop('formset', self.formset)
        language_code = self.get_queryset_language(request)

        if self.sortable_field_name:
            class FormSet(BaseFormSet):
                sortable_field_name = self.sortable_field_name
        if language_code:
            class FormSet(BaseFormSet):
                def _construct_form(self, i, **kwargs):
                    form = super()._construct_form(i, **kwargs)
                    form.language_code = language_code 
                    return form
        kwargs['formset'] = FormSet
        return super().get_formset(request, obj, **kwargs)

    def get_queryset_language(self, request):
        return get_language_parameter(request)

After that, you can use instead of NestedTabularInline or NestedStackedInline. If you want to tabular inline just do override to NestedTabularInline.