


modified_count def delete ( self, selector ): return self. insert_one ( kudo ) def update ( self, selector, kudo ): return self. find_one ( selector ) def create ( self, kudo ): return self. find ( selector ) def find ( self, selector ): return self. kudos def find_all ( self, selector ): return self. Import os from pymongo import MongoClient COLLECTION_NAME = 'kudos' class MongoRepository ( object ): def _init_ ( self ): mongo_url = os.

The pymongo library can be installed by running the following commands: To connect and to run queries against the database, you are going to use a library created and maintained by MongoDB itself called pymongo. With the data representation implemented, your next step is to prepare your application to persist data in MongoDB. Persist Your Python REST API with MongoDB Since what your application requires to display the user’s favorited GitHub projects, in other words, what it has to persist in the database is pretty much similar to the incoming request payload, all you had to do for KudoSchema was make it inherits from GitHubRepoSchema and specialized it by adding a new required field user_id which will be used to filter the data in the database by user. Str () class KudoSchema ( GitHubRepoSchema ): user_id = fields. Int ( required = True ) repo_name = fields. You can check your current Python version by running the following command:įrom marshmallow import Schema, fields class GitHubRepoSchema ( Schema ): id = fields.

In this tutorial, you’ll create a simple CRUD application to save and to display your favorite GitHub open source projects. They have software projects that they love and watch closely for the latest changes. Developers all have their favorite GitHub repositories.
