Skip to Content
Django 3 Web Development Cookbook - Fourth Edition
book

Django 3 Web Development Cookbook - Fourth Edition

by Aidas Bendoraitis, Jake Kronika
March 2020
Intermediate to advanced
608 pages
17h 17m
English
Packt Publishing
Content preview from Django 3 Web Development Cookbook - Fourth Edition

How to do it...

Execute the following steps to prepare for multilingual models:

  1. In the core app, create model_fields.py with the following content:
# myproject/apps/core/model_fields.pyfrom django.conf import settingsfrom django.utils.translation import get_languagefrom django.utils import translationclass TranslatedField(object):    def __init__(self, field_name):        self.field_name = field_name    def __get__(self, instance, owner):        lang_code = translation.get_language()        if lang_code == settings.LANGUAGE_CODE:            # The fields of the default language are in the main               model            return getattr(instance, self.field_name)        else:            # The fields of the other languages are in the               translation            # model, but falls back to the main model            translations = instance.translations.filter( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Django 2 Web Development Cookbook - Third Edition

Django 2 Web Development Cookbook - Third Edition

Jake Kronika, Aidas Bendoraitis

Publisher Resources

ISBN: 9781838987428Supplemental Content