pythondjangodjango-parler

How to: Django Parler CRUD Forms


I am using Parler for localizing my models.

Django Admin works fine, but now I want to recreate the admin forms in the frontend.

So far, I can create a new model but it is always created in the default language. The question now is, how can I set the language?

Best case scenario would be, that I can select the language via <select> in the form, but the default value would be set by a get param language=iso_code or if it is way easier using the language tabs like in Django Admin.

EDIT: The problem seems to be somewhere in the form class.

Model

class Category(MPTTModel, TranslatableModel):
    title = TranslatedField(any_language=True)
    description = TranslatedField()
    slug = TranslatedField()
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children',
                            db_index=True)

    objects = CategoryManager()

    def save(self, *args, **kwargs):
        super(Category, self).save(*args, **kwargs)


class CategoryTranslation(TranslatedFieldsModel):
    title = models.CharField(max_length=200)
    description = models.TextField(null=True)
    slug = models.SlugField()

    master = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='translations')

    class Meta:
        unique_together = (
            ('language_code', 'slug'),
        )

View

@method_decorator(login_required, name='dispatch')
class CategoriesCreateView(TranslatableCreateView):
    model = Category
    context_object_name = 'categories'
    template_name = 'categories/update.html'
    form_class = CategoryForm
    object = Category()

    def get(self, request, *args, **kwargs):
        request.DEFAULT_LANGUAGE = LANGUAGE_CODE
        request.meta_title = _('Create Category')
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        request.DEFAULT_LANGUAGE = LANGUAGE_CODE
        request.meta_title = _('Create Category')
        return super().post(request, *args, **kwargs)

Form

class CategoryForm(TranslatableModelForm):
    use_required_attribute = False

    title = TranslatedField()
    description = TranslatedField()
    slug = TranslatedField()
    parent = TreeNodeChoiceField(queryset=Category.objects.all())

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.form_action = '.'
        self.helper.form_class = 'form-horizontal form-bordered'
        self.helper.label_class = 'col-lg-3'
        self.helper.field_class = 'col-lg-8'

        self.helper.layout = self.__get_layout()
        super().__init__(*args, **kwargs)
        self.fields['parent'].required = False

    @staticmethod
    def __get_layout():
        layout = Layout(
            Field('parent'),
            Field('title'),
            Field('slug'),
            Field('description'),
            ButtonHolder(
                Submit('submit', _('Save'))
            )
        )
        return layout

    class Meta:
        model = Category
        fields = ['title', 'description', 'slug', 'parent']

Solution

  • Damn of course the solution is super simple.

    The problem was, that self.helper.form_action = '.' overrides all get params, which included the language/translation.