File: performance.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 (55 lines) | stat: -rw-r--r-- 1,735 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
44
45
46
47
48
49
50
51
52
53
54
55
Performance guidelines
======================

The translations of each model is stored in a separate table.
In some cases, this may cause in N-query issue.
*django-parler* offers two ways to handle the performance of the dabase.

Caching
-------

All translated contents is cached by default.
Hence, when an object is read again, no query is performed.
This works out of the box when the project uses a proper caching:

.. code-block:: python

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
            'KEY_PREFIX': 'mysite.production',  # Change this
            'LOCATION': '127.0.0.1:11211',
            'TIMEOUT': 24*3600
        },
    }

You have to make sure your project has the proper backend support available::

    pip install python-memcached

Now, the translation table only has to be read once per day.

Query prefetching
-----------------

By using :func:`~django.db.models.query.QuerySet.prefetch_related`,
all translations can be fetched in a single query:

.. code-block:: python

    object_list = MyModel.objects.prefetch_related('translations')
    for obj in object_list:
        print obj.title  # reads translated title from the prefetched queryset

Note that the prefetch reads the information of all languages,
not just the currently active language.

When you display translated objects in a form, e.g. a select list, you can prefetch the queryset too:

.. code-block:: python

    class MyModelAdminForm(TranslatableModelForm):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['some_field'].queryset = self.fields['some_field'].queryset.prefetch_related('translations')