Skip to content

Commit

Permalink
feat: Add global MODELTRANSLATION_REQUIRED_LANGUAGES setting
Browse files Browse the repository at this point in the history
Refs #743
  • Loading branch information
last-partizan authored Jun 20, 2024
1 parent b726c8f commit 0bbdb5f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
13 changes: 9 additions & 4 deletions docs/modeltranslation/registration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ say it in code::
Of course multiple inheritance and inheritance chains (A > B > C) also work as
expected.

However, if the base class is not abstract, inheriting the ``TranslationOptions`` will
cause errors, because the base ``TranslationOptions`` already took care of adding
fields to the model. The example below illustrates how to add translation fields to a
However, if the base class is not abstract, inheriting the ``TranslationOptions`` will
cause errors, because the base ``TranslationOptions`` already took care of adding
fields to the model. The example below illustrates how to add translation fields to a
child model with a non-abstract base::

from modeltranslation.translator import translator, TranslationOptions
Expand All @@ -119,7 +119,7 @@ child model with a non-abstract base::
translator.register(NewsWithImage, NewsWithImageTranslationOptions)


This will add the translated fields ``title`` and ``text`` to the ``News`` model and further add
This will add the translated fields ``title`` and ``text`` to the ``News`` model and further add
the translated field ``image`` to the ``NewsWithImage`` model.

.. note:: When upgrading from a previous modeltranslation version (<0.5), please
Expand Down Expand Up @@ -289,6 +289,11 @@ For German, all fields (both ``title`` and ``text``) are required; for all other

The required fields are still ``null=True``, though.

.. versionadded:: 0.20

To set required_languages for all models, use `MODELTRANSLATION_REQUIRED_LANGUAGES` setting,
which accepts the same values as `required_languages` class variable.


``TranslationOptions`` attributes reference
-------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion modeltranslation/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from ._typing import AutoPopulate
from ._typing import AutoPopulate, _ListOrTuple

TRANSLATION_FILES: tuple[str, ...] = tuple(
getattr(settings, "MODELTRANSLATION_TRANSLATION_FILES", ())
Expand All @@ -21,6 +21,8 @@
raise ImproperlyConfigured("MODELTRANSLATION_DEFAULT_LANGUAGE not in LANGUAGES setting.")
DEFAULT_LANGUAGE = _default_language or AVAILABLE_LANGUAGES[0]

REQUIRED_LANGUAGES: _ListOrTuple[str] = getattr(settings, "MODELTRANSLATION_REQUIRED_LANGUAGES", ())

# Fixed base language for prepopulated fields (slugs)
# (If not set, the current request language will be used)
PREPOPULATE_LANGUAGE: str | None = getattr(settings, "MODELTRANSLATION_PREPOPULATE_LANGUAGE", None)
Expand Down
4 changes: 3 additions & 1 deletion modeltranslation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ class TranslationOptions(metaclass=FieldsAggregationMetaClass):
``related_fields`` contains names of reverse lookup fields.
"""

required_languages: ClassVar[_ListOrTuple[str] | dict[str, _ListOrTuple[str]]] = ()
required_languages: ClassVar[_ListOrTuple[str] | dict[str, _ListOrTuple[str]]] = (
mt_settings.REQUIRED_LANGUAGES
)

def __init__(self, model: Model) -> None:
"""
Expand Down

0 comments on commit 0bbdb5f

Please sign in to comment.