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 enum import Enum
4from pydantic import BaseModel
5
6
7############################################
8# Enumerations are defined here
9############################################
10
11############################################
12# Classes are defined here
13############################################
14class AuthorCreate(BaseModel):
15 name: str
16 email: str
17 publishes: List[int]
18
19class LibraryCreate(BaseModel):
20 address: str
21 name: str
22
23class BookCreate(BaseModel):
24 title: str
25 pages: int
26 release: date
27 writtenBy: List[int]
28 locatedIn: int # N:1 Relationship