Let’s explore some examples of managing the Pydantic default values in our data models, along with various methods that we can use to interact with those default values.
Example 1: Customizing the User Settings with the Pydantic Data Model
Suppose we’re building an application where the users can customize their settings. We want to create a Pydantic data model that represents these user settings, including some default values that the users can change.
To implement this example, we need to import the Pydantic library to help us define and manage our data model. We write the following line of code to import the library:
The “pydantic import BaseModel” line is an import statement that brings in the “BaseModel” class from the Pydantic library. This class is a fundamental part of Pydantic and is used to create the data models that help to ensure the data correctness and consistency.
Now, we create the data model class. We define what pieces of information each user setting can have. Let’s say each user setting has a username, whether they want to receive notifications, and how often they want them (in hours).
Here, the “UserSet(BaseModel)” is the data model class. The “username” is a text that represents the user’s name. The “get_notifications” switch can be either True or False. By default, it’s set to True which means that the users want to get notifications. And “notif_freq” is a number (integer) that shows how often the notifications are sent. It’s set to 24.
get_notifications: bool = True
notif_freq: int = 24
After setting the data class’s structure, we use our data model to create specific user settings with their own details.
We make a user setting instance named “U1_settings” for a user named “Adam”. Since we didn’t say anything about notifications or frequency, the defaults (True for notifications and 24 for frequency) are used.
When we assemble it, the complete code looks like this:
class UserSet(BaseModel):
username: str
get_notifications: bool = True
notif_freq: int = 24
U1_settings = UserSet(username="Adam")
print(U1_settings)
The obtained output of the code is:
We can also customize the settings when creating instances. This is like filling out the form but choosing different options.
We write the following line of code to do this:
In this code, we create another user setting instance named “U2_settings” for a person named “Julia.” We then customize her preferences. The username is specified as “Julia.” She doesn’t want to receive notifications; thus, the “get_notifications” is set to False. And she prefers notifications every 10 hours; therefore, we set the “notif_freq” to 10.
The complete code is:
class UserSet(BaseModel):
username: str
get_notifications: bool = True
notif_freq: int = 24
U2_settings = UserSet(username="Julia", get_notifications=False, notif_freq=10)
print(U2_settings)
The generated result by executing this code snippet is:
In conclusion, we created a way to store the user settings. These settings can include a username, whether they want notifications (which are on by default), and how often they want those notifications (which defaults to every 24 hours). You can then create specific instances with these settings for different users.
Example 2: Setting the Default Values in the Product Configuration Model
Now, we create another example to comprehend the concept better. Consider a scenario where we’re working on an e-commerce platform to sell the products. Each product can have various configurations, and we want to create a Pydantic data model to manage these configurations including the default values.
As we did earlier, the first step is importing the Pydantic library to use its features for creating and validating the data models.
In the next step, we create a template for how the product configurations should look using the Pydantic library. This template ensures that each product configuration has the right structure and follows specific rules.
p_name: str
is_on_discount: bool = False
discount_perc: float = 0.0
We made a “ProdConfig” class in the previous code snippet. In this class, we have “p_name” which is a string that represents the product’s name. The “is_on_discount” is a Boolean (either True or False) that indicates whether the product has a discount. By default, it’s set to False. Lastly, we have the “discount_perc” which is a floating-point number that represents the discount percentage. By default, it’s set to 0.0.
After setting the structure of the product configuration, we create the instances of the “ProductConfig” class to represent the different product configurations.
Let’s understand what’s happening in this code.
The “prod1” is the name that we give to the instance that we create. The “ProductConfig(p_name=”Clock”)” is where we’re actually creating the instance. The “p_name” is where we have to specify the name of the product.
So, the previous line of code creates an instance named “prod1” based on the “ProductConfig” template. The “p_name” is “Clock” and since you didn’t specify anything about discounts or discount percentages, the default values are used.
Here is the complete code:
class ProdConfig(BaseModel):
p_name: str
is_on_discount: bool = False
discount_perc: float = 0.0
prod1 = ProdConfig(p_name=" Clock")
print(prod1)
The generated output is:
Similarly, we create another instance named “prod2” by utilizing the “ProdConfig” data model.
This line of code creates a specific product instance named “prod2” where the product’s name is defined as “Book” and we specify that the product is on discount (based on is_on_discount=True), and the discount percentage for it is 10.0%.
This example showcases how we can use Pydantic to create a structured data model for product configurations. We set the default values that provide a starting point, and we can then customize these settings when creating instances.
The complete code for observation is provided here:
class ProdConfig(BaseModel):
p_name: str
is_on_discount: bool = False
discount_perc: float = 0.0
prod2 = ProdConfig(p_name="Book", is_on_discount=True, discount_perc=10.0)
print(prod2)
The output is:
Conclusion
Pydantic allows you to create structured, consistent data models and set the initial attribute default values. This article covered two examples to comprehend the procedure to manage the Pydantic default values in data models. The first illustration explained how we can manage and use the settings in an application using the Pydantic model. The second example elaborated on a scenario where an e-commerce platform wants to set the default values in the product configuration model.