Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Pydantic, adding spacing between fields in a model is done using the Config class.

  1. First, you need to import BaseSettings and Field from Pydantic:

    from pydantic import BaseSettings, Field
    
  2. Then, create your model and add the fields with their respective data types:

    class MyModel(BaseSettings):
       name: str
       age: int
       email: str
    
  3. 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.