1 | initial version |
In Pydantic, adding spacing between fields in a model is done using the Config
class.
First, you need to import BaseSettings
and Field
from Pydantic:
from pydantic import BaseSettings, Field
Then, create your model and add the fields with their respective data types:
class MyModel(BaseSettings):
name: str
age: int
email: str
Finally, add the Config
class to your model and set the fields
attribute to a dictionary containing the field names as keys and their configurations as values. To add spacing between fields, use the extra
key and set it to "forbid"
:
class MyModel(BaseSettings):
name: str
age: int
email: str
class Config:
fields = {
"name": {"alias": "Name"},
"age": {"alias": "Age"},
"email": {"alias": "Email", "extra": {"forbid": True}}
}
In this example, the extra
key is set to "forbid"
for the email
field. This prevents any extra properties from being added to the field, thus creating spacing between it and the previous field (age
).
You can also use other attributes in the Config
class to configure your fields, such as alias
, title
, and description
.