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
.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-05-15 11:00:00 +0000
Seen: 8 times
Last updated: May 29 '22
What is the method for showcasing additional fields using a foreign key in Laravel?
In Django, why are the fields from the parent class not included in the table of the child class?
What is the reason for the appearance of a row when all fields are null?
How can the TextInput boxes be aligned at the center of the page?
How can one search for related fields in reverse order in Supabase?
When <Tvalue> contains null fields, why does PostAsJsonAsync fail to call the Controller?
What is the approach to display input forms in a consecutive manner?