File: fallback.rst

package info (click to toggle)
python-django-parler 2.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,032 kB
  • sloc: python: 4,293; makefile: 164; sh: 6
file content (43 lines) | stat: -rw-r--r-- 1,599 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Translations without fallback languages
=======================================

When a translation is missing, the fallback languages are used.
However, when an object has no fallback languages, this still fails.

There are a few solutions to this problem:

1. Declare the translated attribute explicitly with ``any_language=True``::

        from parler.models import TranslatableModel
        from parler.fields import TranslatedField

        class MyModel(TranslatableModel):
            title = TranslatedField(any_language=True)

   Now, the title will try to fetch one of the existing languages from the database.

2. Use :func:`~parler.models.TranslatableModel.safe_translation_getter` on attributes
   which don't have an ``any_language=True`` setting. For example::

        model.safe_translation_getter("fieldname", any_language=True)

3. Catch the :class:`~parler.models.TranslationDoesNotExist` exception. For example::

        try:
            return object.title
        except TranslationDoesNotExist:
            return ''

   Because this exception inherits from :class:`~exceptions.AttributeError`,
   templates already display empty values by default.

4. Avoid fetching untranslated objects using queryset methods. For example::

        queryset.active_translations()

   Which is almost identical to::

        codes = get_active_language_choices()
        queryset.filter(translations__language_code__in=codes).distinct()

   Note that the same `ORM restrictions <https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships>`_ apply here.