django-modeltranslation icon indicating copy to clipboard operation
django-modeltranslation copied to clipboard

Is it possible to use django-modeltranslation outside of admin?

Open nurzhannogerbek opened this issue 8 years ago • 8 comments

Hello! In my Django project I want to use your app. I successfully install app and make settings, also registered models for translation as it was recommended in docs.

Question: Is it possible to use this app outside of admin? If it possible what I need to do? I am little bit comfused.

translation.py:

class ArticleTranslationOptions(TranslationOptions):
    fields = ('title', 'body',)

translator.register(Article, ArticleTranslationOptions)

nurzhannogerbek avatar Aug 31 '17 16:08 nurzhannogerbek

Yes, you can. In read mode you will see the translated field In write mode, if you want to set a field for every language, you have to customize your form init to add the field for every language that you have enabled in your settings. Same for serializers if you are using djangorestframework

The registration you have setted in translation.py enables the migrations to chenge your db, not the admin visualization, that is enabled by inheriting you ModelAdmin from ModelTranslationAdmin

madEng84 avatar Sep 01 '17 00:09 madEng84

I am interested in write mode (Create/Edit Form). Is this code would be enough or I need to do something else? I have next error with this code. Can you say whats wrong?!

settings.py:

LANGUAGE_CODE = 'ru'

LANGUAGES = (
    ('ru', _('Russian')),
    ('en', _('English')),
    ('de', _('German')),
)

MODELTRANSLATION_LANGUAGES = ('en', 'de')

forms.py:

from modeltranslation.forms import TranslationModelForm

class ArticleForm(TranslationModelForm):
    """
        Form based on "Article" model.
    """

    class Meta:
        model = Article
        fields = ('title', 'title_en', 'title_de', 'body', 'body_en', 'body_de',)

    def __init__(self, *args, **kwargs):
        super(ArticleForm, self).__init__(*args, **kwargs)
        self.fields['title'].widget.attrs = {
            'class': 'form-control',
            'id': 'title',
        }
        self.fields['title_en'].widget.attrs = {
            'class': 'form-control',
            'id': 'title_en',
        }
        self.fields['title_de'].widget.attrs = {
            'class': 'form-control',
            'id': 'title_de',
        }
        self.fields['body'].widget.attrs = {
            'class': 'form-control',
            'id': 'opt_head',
        }
        self.fields['body_en'].widget.attrs = {
            'class': 'form-control',
            'id': 'body_en',
        }
        self.fields['body_de'].widget.attrs = {
            'class': 'form-control',
            'id': 'body_de',
        }

ERROR:

Traceback (most recent call last):
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Nurzhan\PycharmProjects\CA\slider\views.py", line 41, in get
    slide_create_form = SlideForm()
  File "C:\Users\Nurzhan\PycharmProjects\CA\slider\forms.py", line 29, in __init__
    'id': 'title_en',
KeyError: 'title_en'

nurzhannogerbek avatar Sep 01 '17 13:09 nurzhannogerbek

When I try to use ModelForms instead of TranslationModelForm it shows in form only fields: title and body. How to show other fields in form?

nurzhannogerbek avatar Sep 03 '17 19:09 nurzhannogerbek

If you want to show all fields, use ModelForm. TranslationModelForm is intended to hide all transaltion fields: http://django-modeltranslation.readthedocs.io/en/latest/forms.html Also please check if form is not being registered too early: http://django-modeltranslation.readthedocs.io/en/latest/registration.html#precautions-regarding-registration-approach

zlorf avatar Sep 03 '17 20:09 zlorf

Right now I have fields in my forms but steal have strange problem in edit form.

title is original field (Russian). Other fields are title_en (English), title_de (German). When I open edit form title and title_en fields are the same despite the title field must be in Russian but in fact they both are in English. Why title field duplicate the value of title_en? In the same time title_de is correct. How to change this strange behavior?

Is my settings.py file correct?

nurzhannogerbek avatar Sep 04 '17 06:09 nurzhannogerbek

Use title_ru. The original field (title) is not containing any specific language. It rather shows/writes current active language.

zlorf avatar Sep 04 '17 06:09 zlorf

Well, I can use title_ru field. Its not problem, but can I stop overwrite of title field?! I want to show that value which was at start in title field despite of what language was chosen.

nurzhannogerbek avatar Sep 04 '17 06:09 nurzhannogerbek

No, you cannot stop it. Again, it's in the docs: http://django-modeltranslation.readthedocs.io/en/latest/usage.html#the-state-of-the-original-field However, you can always tweak the form to assign a value of your choice to title input/field

zlorf avatar Sep 04 '17 16:09 zlorf