Write Data to MongoDB
You can use your application's models to update documents
stored in the sample_mflix
database. To update documents,
enter the Python interactive shell and call create, update,
and delete functions on your model objects.
Insert a movie into the database
Run the following code to create an Movie
object that
stores data about a movie titled "Minari"
, including
its awards in an Award
object:
movie_awards = Award(wins=122, nominations=245, text="Won 1 Oscar") movie = Movie.objects.create( title="Minari", plot="A Korean-American family moves to an Arkansas farm in search of their own American Dream", runtime=217, released=timezone.make_aware(datetime(2020, 1, 26)), awards=movie_awards, genres=["Drama", "Comedy"] )
Insert a Viewer into the database
You can also use your Viewer
model to insert documents into the
sample_mflix.users
collection. Run the following code to create
a Viewer
object that stores data about a movie viewer named "Abigail Carter"
:
viewer = Viewer.objects.create( name="Abigail Carter", email="abigail.carter@fakegmail.com" )
Render your new objects
To ensure that you inserted a Movie
object into the database,
visit the http://127.0.0.1:8000/recent_movies/ URL.
You can see a list of five movies in the sample_mflix.movies
database, with your new movie listed at the top.
Then, ensure that you inserted a Viewer
object into the
database by visiting the http://127.0.0.1:8000/viewers_list/
URL. You can see a list of ten viewer names in the sample_mflix.users
database, with your new viewer listed at the top. Ensure that the
viewer named "Alliser Thorne", deleted in a previous step, does not appear
in this list.
After completing these steps, you have inserted and edited documents
in the sample_mflix
sample database.