Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating Models from Other Objects

00:00 Creating Models From Other Objects Pydantic’s BaseModel is equipped with a suite of methods that make it easy to create models from other objects such as dictionaries and JSON.

00:11 For example, if you want to instantiate an employee object from a dictionary, you can use the model_validate class method. First, you create new_employee_dict, a dictionary with your employee fields.

00:40 Then you pass it into Employee.model_validate to create an employee instance. Under the hood, Pydantic validates each dictionary entry to ensure it conforms with the data you’re expecting.

00:54 If any of the data is invalid, Pydantic will throw an error in the same way you saw previously. You’ll also be notified if any fields are missing from the dictionary.

01:04 You can do the same thing with JSON objects using the model_validate_json method. First, you create new_employee_json, a valid JSON string that stores the employee fields.

01:16 Then

01:39 you use the model_validate_json method to validate and create an employee object from new_employee_json.

01:55 While it may seem a subtle difference, the ability to create and validate Pydantic models from JSON is powerful because JSON is one of the most popular ways to transfer data across the Web.

02:06 This is one of the reasons why FastAPI relies on Pydantic to create REST APIs. You can also serialize Pydantic models as dictionaries or JSON.

02:19 You use the model_dump method to convert new_employee to a dictionary and use the model_dump_json method to convert it to a JSON string.

02:31 Notice how the model_dump_json method returns a JSON object with date of birth and department stored as strings.

02:40 While Pydantic already validated these fields and converted your model to JSON, whoever uses this JSON downstream won’t know that date of birth needs to be a valid date, and the department needs to be a category in your department enum.

02:53 To solve this, you can create a JSON schema from the employee model. JSON schemas tell you what fields are expected and what values are represented in a JSON object.

03:04 You can think of this as the JSON version of your employee class definition. Here’s how you generate a JSON schema for Employee.

03:15 You call the model_json_schema method and get a dictionary representing the model’s JSON schema. For clarity here it is made human-readable on screen.

03:26 The first entry you see shows you the values that department can take on. You also see information about how your field should be formatted. For instance, according to this JSON schema, employee_ID is expected to be a UUID and date_of_birth is expected to be a date.

03:46 You can convert your JSON schema to a JSON string using json.dumps() which enables just about any programming language to validate JSON objects produced by your employee model.

03:57 In other words, not only can Pydantic validate incoming data and serialize it as JSON, but it also provides other programming languages with the information they need to validate your model’s data via JSON schemas.

04:09 With that, you now understand how to use Pydantic’s base model to validate and serialize your data, and in the next section of the course, you’ll learn how to use fields to further customize your validation.

Become a Member to join the conversation.