Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global required fields #743

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading