Giter Site home page Giter Site logo

Comments (5)

vdboor avatar vdboor commented on June 30, 2024

Well, the models can handle it for sure. I'm just not sure if that can be done in the admin but it's worth a try! Would be really cool to have! :-)

I've wanted this to work in standard Django forms for some time now, because the parler-oscar integration does suffer from exposing just one language in the form. Having a form class that can generate fields for all languages would be a first good start to implement this!

from django-parler.

svezina avatar svezina commented on June 30, 2024

+1
Having only one language at once with the form class can cause problems. It would be nice if the form class automatically showed all languages. "Description (EN), Description (FR), etc.".

I find myself programming manual forms everytime there's a multilingual component.

from django-parler.

svezina avatar svezina commented on June 30, 2024

Here is how I do my forms it in the meantime.
This is an example where "name" and "description" are multilingual fields.

from parler.forms import TranslatableModelForm, TranslatedField
from django.utils.translation import gettext_lazy as _
from django.conf import settings

class MyForm(TranslatableModelForm):

    #add all multilingual fields to the form
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        for lang_code, lang_name in settings.LANGUAGES:
            #I have no clue why this doesn't work without the try/except.
            try:
                self.instance.set_current_language(lang_code)
                self.fields["name_"+lang_code] = forms.CharField(label=_("Name ")+" ("+lang_code+")", max_length=100, required=True, initial=self.instance.name)
            except:
                self.instance.set_current_language(lang_code)

    #Save all multilingual fields
    def save(self, commit=True):
        instance = super(MyForm, self).save(commit=False)
        instance.save()
        for lang_code, lang_name in settings.LANGUAGES:
            instance.set_current_language(lang_code)
            instance.name = self.cleaned_data['name_'+lang_code]
            instance.save()
        return instance

    class Meta:
        model = MyModel
        fields = '__all__'
        exclude = ("name", "description",) #exclude multilingual fields because they are dealt with above

from django-parler.

vstoykov avatar vstoykov commented on June 30, 2024

@svezina what is the exception that you are catching? Probably by knowing what is the exception someone can understand why happens.

from django-parler.

slomo avatar slomo commented on June 30, 2024

I think the issue here is getting values from the translated instance for the initial data. That will fail upon creation and if a field is not set yet in the model. At least that is what I have with django 1.8 and parler 1.6.4

mymodel does not have a translation for the current language!
MyModel ID #5, language=de (tried fallbacks en)
Attempted to read attribute title.

from django-parler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.