Using Models
00:02 Pydantic’s primary way of defining data schemas is through models. A Pydantic model is an object similar to a Python data class that defines and stores data about an entity with annotated fields.
00:15 Unlike data classes, Pydantic’s focus is centered around automatic data parsing, validation, and serialization. The best way to understand this is to create your own models, and that’s what you’ll be doing next.
00:28 Let’s say you are building an application used by a human resources department to manage employee information and you need a way to verify that new employee information is in the correct form.
00:39 For example, each employee should have an ID, name, email, date of birth, salary, department, and benefit selection, and this is a perfect use case for a Pydantic model.
00:51
To define your employee model, you create a class that inherits from Pydantic’s BaseModel
. First, you import the dependencies you need to define your employee model.
01:09
Then you create an Enum
to represent the different departments in the company, and you’ll use this to annotate the department field in your employee model.
01:29
Here you define your Pydantic model Employee
, which inherits from BaseModel
and defines the names and expected types of the employee fields via annotations. employee_id:
This is the UUID for the employee you want to store information for.
01:45
By using the UUID annotation, Pydantic ensures that this field is always a valid UUID. Each instance of Employee
will be assigned a UUID by default, as you specified by calling uuid4()
.
02:01
name:
the employee’s name, which Pydantic expects to be a string.
02:06
email:
Pydantic will ensure that each employee email is valid by using Python’s email-validator
library. date_of_birth:
Each employee’s date of birth must be a valid date as annotated by date
from Python’s datetime
module.
02:22
If you pass a string into date_of_birth
, Pydantic will attempt to parse and convert it into a date object. salary:
This is the employee’s salary and it’s expected to be a float. department:
Each employee’s department must be one of HR, Sales, IT, or Engineering as defined in the department Enum
.
02:44
elected_benefits:
This field stores whether the employee has elected benefits and Pydantic expects it to be a Boolean. The simplest way to create an employee object is to instantiate it as you would any other Python object.
02:58 To do this, open a Python REPL and run the code seen on screen.
03:04
First, you import Employee
and then you create an object with all of the required employee fields.
03:28
Pydantic successfully validates and coerces the fields you passed in and it creates a valid employee object. Notice how Pydantic automatically converts the date string into a date object and the IT string to its respective department Enum
.
03:44 Next, you’ll look at how Pydantic responds when you try to pass invalid data to an employee instance.
04:10 Pydantic gives you a detailed error message for each field, telling you what was expected, what was received, and where you can go to learn more about the error.
04:19
As there are quite a few errors that presented on this slide to make them easier to read. This detailed validation is powerful because it prevents you from storing invalid data in Employee
.
04:30 It also gives you confidence that the employee objects you instantiate without errors contain the data you’re expecting and you can trust this data downstream in your code or other applications.
04:42 In the next section of the course, you’ll see another useful feature of Pydantic: creating models from other objects.
Become a Member to join the conversation.