Pydantic Classes Generator#

This code generator produces Pydantic classes, which represent the entities and relationships of a B-UML model. These Pydantic classes can be utilized by other code generators to generate code that uses Pydantic classes, such as REST API Generator and Backend Generator.

Let’s generate the code for the Pydantic classes of our Structural model example B-UML model example. You should create a PydanticGenerator object, provide the B-UML model, and use the generate method as follows:

from besser.generators.Pydantic_classes import PydanticGenerator

generator: PydanticGenerator = Pydantic_Generator(model=library_model)
generator.generate()

Upon executing this code, a pydantic_classes.py file containing the Pydantic models will be generated. in the <<current_directory>>/output folder and it will look as follows.

 1from datetime import datetime, date
 2from typing import List, Optional, Union,Set
 3from pydantic import BaseModel
 4
 5############################################
 6#
 7# The classes are defined here
 8#
 9############################################
10
11class BookCreate(BaseModel):
12    pages: int
13    release: datetime
14    title: str
15    authors: Optional[List[Union["AuthorCreate", int]]] = None  # N:M Relationship
16    library_id: int
17
18class AuthorCreate(BaseModel):
19    name: str
20    email: str
21    books: Optional[List[Union["BookCreate", int]]] = None  # N:M Relationship
22
23class LibraryCreate(BaseModel):
24    address: str
25    name: str