diff --git a/django_setup_configuration/documentation/setup_config_example.py b/django_setup_configuration/documentation/setup_config_example.py index 1f91de1..752fe3f 100644 --- a/django_setup_configuration/documentation/setup_config_example.py +++ b/django_setup_configuration/documentation/setup_config_example.py @@ -16,6 +16,9 @@ get_args, get_origin, ) +from django.utils.encoding import force_str +from django.utils.functional import Promise + import ruamel.yaml from docutils import nodes @@ -31,6 +34,13 @@ NO_EXAMPLE = object() +class LazyEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, Promise): + return force_str(obj) + return super().default(obj) + + def extract_literal_values(annotation: Type | None) -> list: """ Checks if the given type annotation is a Literal or contains Literals. @@ -68,11 +78,14 @@ def _get_default_from_field_info(field_info: FieldInfo) -> Any: """ if field_info.default_factory: return field_info.default_factory() - return ( - field_info.default.value - if isinstance(field_info.default, Enum) - else field_info.default - ) + + match field_info.default: + case Promise(): + return force_str(field_info.default) + case Enum(): + return field_info.default.value + case _: + return field_info.default def _yaml_set_wrapped_comment( diff --git a/testapp/models.py b/testapp/models.py index 3e7d2d4..572ddb7 100644 --- a/testapp/models.py +++ b/testapp/models.py @@ -1,5 +1,6 @@ from django.contrib.postgres.fields import ArrayField from django.db import models +from django.utils.translation import gettext_lazy as _ class StrChoices(models.TextChoices): @@ -60,3 +61,4 @@ class DjangoModel(models.Model): models.JSONField(), default=lambda: [{"foo": "bar"}, {"foo": "baz"}] ) array_field = ArrayField(models.JSONField(), null=True, blank=True) + str_with_localized_default = models.TextField(default=_("Localized default")) diff --git a/tests/test_documentation.py b/tests/test_documentation.py index bf266ba..25de461 100644 --- a/tests/test_documentation.py +++ b/tests/test_documentation.py @@ -3,6 +3,7 @@ from typing import Literal, Union from unittest.mock import patch +from django.utils.translation import gettext_lazy as _ import pytest from docutils import nodes from docutils.frontend import get_default_settings @@ -78,6 +79,7 @@ class Meta: "json_with_default_factory", "nullable_str", "int_with_choices_and_blank_and_non_choice_default", + "str_with_localized_default", ) } extra_kwargs = { @@ -259,6 +261,10 @@ def test_directive_output(register_directive, docutils_document): # DEFAULT VALUE: 42 # REQUIRED: false int_with_choices_and_blank_and_non_choice_default: 42 + + # DEFAULT VALUE: "Localized default" + # REQUIRED: false + str_with_localized_default: Localized default """ )