Examples

While the Configuration documentation contains all available options in detail, this page shows them in conjunction to provide different examples on how to display pydantic models and settings.

Default configuration

This example shows the default out-of-the-box configuration of autodoc_pydantic. In contrast, it also shows how standard sphinx autodoc displays the same example code.

pydantic settings target.example_setting.ExampleSettings

Document your project settings very conveniently. Applies like wise to pydantic models.

Show JSON schema
{
   "title": "ExampleSettings",
   "description": "Document your project settings very conveniently. Applies like wise\nto pydantic models.",
   "type": "object",
   "properties": {
      "field_plain_with_validator": {
         "title": "Field Plain With Validator",
         "default": 100,
         "env_names": [
            "foo_field_plain_with_validator"
         ],
         "type": "integer"
      },
      "BarFoo": {
         "title": "Barfoo",
         "default": "FooBar",
         "env_names": [
            "foo_field_with_validator_and_alias"
         ],
         "type": "string"
      },
      "field_with_constraints_and_description": {
         "title": "Field With Constraints And Description",
         "description": "Shows constraints within doc string.",
         "default": 5,
         "minimum": 0,
         "maximum": 100,
         "env_names": [
            "foo_field_with_constraints_and_description"
         ],
         "type": "integer"
      }
   },
   "additionalProperties": false
}

Config
  • allow_mutation: bool = True

  • env_prefix: str = foo_

Fields
Validators
field field_plain_with_validator: int = 100

Show standard field with type annotation.

Validated by
field field_with_constraints_and_description: int = 5

Shows constraints within doc string.

Constraints
  • minimum = 0

  • maximum = 100

field field_with_validator_and_alias: str = 'FooBar' (alias 'BarFoo')

Shows corresponding validator with link/anchor.

Validated by
validator check_max_length_ten  »  field_with_validator_and_alias, field_plain_with_validator

Show corresponding field with link/anchor.

Complete configuration

This example represents a rendered output for which all features are enabled. It deviates from the default configuration above because it contains redundant information which is most likely not required. However, for demonstration purposes, this scenario covers all available display options for pydantic models/settings.

pydantic settings target.example_setting.ExampleSettings

Document your project settings very conveniently. Applies like wise to pydantic models.

Show JSON schema
{
   "title": "ExampleSettings",
   "description": "Document your project settings very conveniently. Applies like wise\nto pydantic models.",
   "type": "object",
   "properties": {
      "field_plain_with_validator": {
         "title": "Field Plain With Validator",
         "default": 100,
         "env_names": [
            "foo_field_plain_with_validator"
         ],
         "type": "integer"
      },
      "BarFoo": {
         "title": "Barfoo",
         "default": "FooBar",
         "env_names": [
            "foo_field_with_validator_and_alias"
         ],
         "type": "string"
      },
      "field_with_constraints_and_description": {
         "title": "Field With Constraints And Description",
         "description": "Shows constraints within doc string.",
         "default": 5,
         "minimum": 0,
         "maximum": 100,
         "env_names": [
            "foo_field_with_constraints_and_description"
         ],
         "type": "integer"
      }
   },
   "additionalProperties": false
}

Config
  • allow_mutation: bool = True

  • env_prefix: str = foo_

Fields
Validators
field field_plain_with_validator: int = 100

Show standard field with type annotation.

Validated by
field field_with_constraints_and_description: int = 5

Shows constraints within doc string.

Constraints
  • minimum = 0

  • maximum = 100

field field_with_validator_and_alias: str = 'FooBar' (alias 'BarFoo')

Shows corresponding validator with link/anchor.

Validated by
validator check_max_length_ten  »  field_with_validator_and_alias, field_plain_with_validator

Show corresponding field with link/anchor.

Validates
model Config
allow_mutation = True
env_prefix = 'foo_'

Fields only

In this scenario everything is hidden except actual pydantic fields. Validators and model/setting config is hidden.

pydantic settings target.example_setting.ExampleSettings

Document your project settings very conveniently. Applies like wise to pydantic models.

Fields
field field_plain_with_validator: int = 100

Show standard field with type annotation.

field field_with_constraints_and_description: int = 5

Shows constraints within doc string.

Constraints
  • minimum = 0

  • maximum = 100

field field_with_validator_and_alias: str = 'FooBar' (alias 'BarFoo')

Shows corresponding validator with link/anchor.