I'm studying Django from the book Django 2 by Examples. I'm trying to improve a project which starts in chapter 10. Now, I'm trying to add multilingualism with the help of "django-parler". In general I did it, but it seems to me that there are better ways. Views are implemented as classes that are inherited from mixins. If a language other than the default language is selected on the page, the form still comes with the value of laguage_code field equal to default. I tried unsuccessfully to change this field in the form_valid method. The form was still saved with the default language. The only option that works for me is this, by it looks like kludge:
def form_valid(self, form):
language = translation.get_language()
_course = form.save(commit=False)
try:
course = Course.objects.get(pk=_course.id)
except Course.DoesNotExist:
course = Course()
course.owner = self.request.user
course.set_current_language(language)
cd = form.cleaned_data
course.subject = cd['subject']
course.title = cd['title']
course.slug = cd['slug']
course.overview = cd['overview']
course.save()
return redirect(reverse('courses:manage_list'))
Maybe someone knows a more elegant way to implement this?