diff --git a/django_setup_configuration/documentation/model_directive.py b/django_setup_configuration/documentation/model_directive.py index b3dd7c0..f0f8c8d 100644 --- a/django_setup_configuration/documentation/model_directive.py +++ b/django_setup_configuration/documentation/model_directive.py @@ -3,7 +3,7 @@ import textwrap from dataclasses import dataclass from enum import Enum -from typing import Annotated, Any, Dict, Type, Union, get_args, get_origin +from typing import Annotated, Any, Dict, Type, Union, get_args, get_origin, Literal import ruamel.yaml from docutils import nodes @@ -75,10 +75,17 @@ def insert_example_with_comments( yaml_set_comment_with_max_length( example_data, field_name, field_info.description, 80, indent=depth * 2 ) + if default := get_default_from_field_info(field_info): example_data.yaml_set_comment_before_after_key( field_name, f"default value: {default}", indent=depth * 2 ) + + if get_origin(field_info.annotation) == Literal: + example_data.yaml_set_comment_before_after_key( + field_name, f"possible values: {get_args(field_info.annotation)}", indent=depth * 2 + ) + example_data.yaml_set_comment_before_after_key( field_name, f"required: {field_info.is_required()}", indent=depth * 2 ) @@ -166,15 +173,16 @@ def process_field_type( ] return PolymorphicExample(example=data, commented_out_examples=other) - # Handle Pydantic models - if isinstance(field_type, type) and issubclass(field_type, BaseModel): - return generate_model_example(field_type, depth=depth) # Handle lists if get_origin(field_type) == list: list_type = get_args(field_type)[0] return [process_field_type(list_type, field_info, field_name, depth + 1)] + # Handle Pydantic models + if isinstance(field_type, type) and issubclass(field_type, BaseModel): + return generate_model_example(field_type, depth=depth) + def generate_basic_example(field_type: Any, field_info: FieldInfo) -> Any: """