Schema validation with pydantic
It's your turn now! Use pydantic to enforce a schema in your MongoDB operations, defining a Movie class that you use to create a new movie you're about to insert. If you made a typo when creating the new movie, pydantic should complain!
Cet exercice fait partie du cours
Introduction to MongoDB in Python
Instructions
- Do the appropriate imports: from
pydanticimportBaseModel, and fromtypingimportOptional. - Complete the definition of the
Movieclass so thatgenreis a list of strings and . - Finish the definition of
new_movie. It was released in 2012 and has a rating of 8.0. - Insert the movie into the movies collection (available as
mov).
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Do the appropriate imports
from ____ import BaseModel
from typing import ____
# Complete definition of Movie class
class Movie(____):
title: str
genre: ____[____]
release_year: int
____: float
won_oscar: ____[bool] = None
# Finish the details
new_movie = ____(
title = "the avengers",
genre = ["action", "adventure", "sci-fi"],
____ = ____,
rating = ____
)
# Insert the movie into the movies collection (mov)
mov.____