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!
Diese Übung ist Teil des Kurses
Introduction to MongoDB in Python
Anleitung zur Übung
- Do the appropriate imports: from
pydantic
importBaseModel
, and fromtyping
importOptional
. - Complete the definition of the
Movie
class so thatgenre
is 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
).
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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.____