Skip to content

templatepy.demo.pydantic_demo ⚓︎

This module demonstrates the use of pydantic in Python.

CLASS DESCRIPTION
Address

Address model with validation.

Profile

Profile model with nested models.

User

Complete user model with additional fields.

UserBase

Base user model with common attributes.

UserCreate

Model for creating a new user.

FUNCTION DESCRIPTION
create_sample_profile

Create a sample profile with nested models.

create_sample_user

Create a sample user for demonstration.

templatepy.demo.pydantic_demo.Address pydantic-model ⚓︎

Bases: BaseModel

Address model with validation.

PARAMETER DESCRIPTION
street

Street address including house number

TYPE: str

city

City name

TYPE: str

country

Country name

TYPE: str

postal_code

US postal code in format: 12345 or 12345-6789

TYPE: str

Fields:

city pydantic-field ⚓︎

city: str

City name

country pydantic-field ⚓︎

country: str

Country name

postal_code pydantic-field ⚓︎

postal_code: str

US postal code in format: 12345 or 12345-6789

street pydantic-field ⚓︎

street: str

Street address including house number

templatepy.demo.pydantic_demo.Profile pydantic-model ⚓︎

Bases: BaseModel

Profile model with nested models.

PARAMETER DESCRIPTION
user

User information

TYPE: User

address

User's address details

TYPE: Address | None DEFAULT: None

bio

User's biography - maximum 500 characters

TYPE: str | None DEFAULT: None

followers

List of users following this profile

TYPE: List[User] DEFAULT: <dynamic>

Fields:

address pydantic-field ⚓︎

address: Optional[Address] = None

User's address details

bio pydantic-field ⚓︎

bio: Optional[str] = None

User's biography - maximum 500 characters

followers pydantic-field ⚓︎

followers: List[User]

List of users following this profile

user pydantic-field ⚓︎

user: User

User information

templatepy.demo.pydantic_demo.User pydantic-model ⚓︎

Bases: UserBase

Complete user model with additional fields.

PARAMETER DESCRIPTION
name

User's full name - must contain a space

TYPE: str

email

User's email address

TYPE: str

age

User's age - must be between 0 and 120

TYPE: int

is_active

Whether the user account is active

TYPE: bool DEFAULT: True

id

Unique identifier for the user

TYPE: int

created_at

Timestamp when the user was created

TYPE: datetime DEFAULT: <dynamic>

tags

List of tags associated with the user

TYPE: List[str] DEFAULT: <dynamic>

website

User's personal website URL

TYPE: HttpUrl | None DEFAULT: None

Fields:

Validators:

created_at pydantic-field ⚓︎

created_at: datetime

Timestamp when the user was created

id pydantic-field ⚓︎

id: int

Unique identifier for the user

tags pydantic-field ⚓︎

tags: List[str]

List of tags associated with the user

website pydantic-field ⚓︎

website: Optional[HttpUrl] = None

User's personal website URL

name_must_contain_space pydantic-validator ⚓︎

name_must_contain_space(v: str) -> str

Validate that the name contains a space.

templatepy.demo.pydantic_demo.UserBase pydantic-model ⚓︎

Bases: BaseModel

Base user model with common attributes.

PARAMETER DESCRIPTION
name

User's full name - must contain a space

TYPE: str

email

User's email address

TYPE: str

age

User's age - must be between 0 and 120

TYPE: int

is_active

Whether the user account is active

TYPE: bool DEFAULT: True

Fields:

age pydantic-field ⚓︎

age: int

User's age - must be between 0 and 120

email pydantic-field ⚓︎

email: str

User's email address

is_active pydantic-field ⚓︎

is_active: bool = True

Whether the user account is active

name pydantic-field ⚓︎

name: str

User's full name - must contain a space

templatepy.demo.pydantic_demo.UserCreate pydantic-model ⚓︎

Bases: UserBase

Model for creating a new user.

PARAMETER DESCRIPTION
name

User's full name - must contain a space

TYPE: str

email

User's email address

TYPE: str

age

User's age - must be between 0 and 120

TYPE: int

is_active

Whether the user account is active

TYPE: bool DEFAULT: True

password

User's password - minimum 8 characters

TYPE: str

Fields:

password pydantic-field ⚓︎

password: str

User's password - minimum 8 characters

templatepy.demo.pydantic_demo.create_sample_profile ⚓︎

create_sample_profile() -> Profile

Create a sample profile with nested models.

templatepy.demo.pydantic_demo.create_sample_user ⚓︎

create_sample_user() -> User

Create a sample user for demonstration.