Creating users

Now that we have our Nameko dependency in place, we can start to add functionality to our UserWrapper. We will start by creating users. Add the following to the UserWrapper class:

def create(self, **kwargs): 
    user = User(**kwargs) 
    self.session.add(user) 
    self.session.commit() 

This create method will create a new User object, add it to our database session, commit that change to the database, and return the user. Nothing fancy here! But let's talk about the process of self.session.add and self.session.commit. When we first add the user to the session, this adds the user to our local database session in memory, rather than adding them to our actual database. The new user has been staged but no changes have actually taken place ...

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.