Skip to content

Commit

Permalink
support examples
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Dec 20, 2024
1 parent d2baec8 commit 1c0623a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions django_setup_configuration/documentation/model_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
from docutils import nodes
from docutils.parsers.rst import Directive
from pydantic import BaseModel
from typing import Type
from typing import Type, Dict, Any
from django_setup_configuration.fields import DjangoModelRefInfo
from sphinx.directives.code import CodeBlock


def generate_model_example(model: Type[BaseModel]) -> dict:
def generate_model_example(model: Type[BaseModel]) -> Dict[str, Any]:
example_data = {}

# Loop through the annotations of the model to create example data
for field_name, field_type in model.__annotations__.items():
field_info = model.model_fields.get(field_name)
field_description = field_info.description if field_info else None

if isinstance(field_type, DjangoModelRefInfo):
# For DjangoModelRef, provide a mock string (model reference)
example_data[field_name] = "mock_django_model_reference"

elif isinstance(field_type, type) and isinstance(field_type, BaseModel):
if field_info.examples:
example_data[field_name] = field_info.examples[0]
continue

elif isinstance(field_type, type) and issubclass(field_type, BaseModel):
# For nested Pydantic models, create an example using that model
example_data[field_name] = generate_model_example(field_type)

Expand Down
3 changes: 3 additions & 0 deletions django_setup_configuration/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def __init__(
"validate_return"
] = validate_defaults

if examples := kwargs.get("examples"):
field_info_creation_kwargs["examples"] = examples

return super().__init__(**field_info_creation_kwargs)

@staticmethod
Expand Down

0 comments on commit 1c0623a

Please sign in to comment.