10 min read Author: klaas

Build a super simple todo app - Part 1: Adding a Model

Working with models is super easy in PythonOnWheels

Whenever you start to work on an idea and the according application, the first thing you want to do is probably to manage, store and retreive some data. In PythonOnWheels (PoW) this is super easy. All data you manage is organized in (data)models. Let's assume we want to create the worlds best Todo list app. So we need to store todos.

In this hands on tutorial you will learn how PythonOnWheels manages data models.

PythonOnWheels data models directly support many SQL and NoSQL DBs. For SQL PoW uses sqlalchemy under the hood and for NoSQL we support (TinyDB, MongoDB and Elastic)

But the really cool thing is that the model definition is always the same.

Whether you are defining a SQL model or a NoSQL model. Model schema definitions in PythonOnWheels always look like this:

class SomeModel(TinyModel):
schema = {
'title': { 'type': 'string', 'maxlength' : 35 },
'text' : { 'type': 'string'},
'done' : { 'type': 'boolean', "default" : False },
"votes" : { "type" : "integer", "default" : 0 }
}

You see that the syntax is very straightforward. And it gets even better because this in not only some dictionary like definition style, but actually

PythonOnWheels uses cerberus schema definitions for models.

So we have schema definition based on a very reliable python library and we also have validation directly onboard!

Can this get even better ? Yes ! ;)

In the next steps we will create our first model. We use tinyDB here to utilize the easiness of document oriented DBs. In contrast to SQL DBs, document oriented (aka NoSQL DBs) directly support types like lists or dicts. TinyDB is a pure python implementation of such a DB which combines the easiness of working with enhanced types and the seamless flow of file based DBs. You can think of tinyDB as the Sqlite for NoSQL. TinyDB comes with PythonOnWheels included. So no DB setup or installation is needed.

So let's go ahead and setup a tinyDB model for our todo app.

Prerequisits: You should have generated an app (called todo). If you don't know how this works read this (2 Minutes)

1. Generate a todo model

To generate a model just use the generate_model script. Give it a name (-n) and a type (-t).

python generate_model.py -n todo -t tinydb

This will generate the model for you in the models/tinydb subdirectory

2. Let's check what has been generated for us

The model is generated for you with some default attributes and constraints to make it easy for you to see the syntax and common conventions.

This is how it looks. (Hopefully a whole lot is self explanatory)

As you can see, the model class name is the name that you have chosen, and the mode baseclass is the right baseclass for your database of choice. We chose TinyDB in this case, which is a really great file based NoSQL DB. Sort of the Sqlite for NoSQL.

PyhtonOnWheels supports many Databases, NoSQL and SQL. The important thing is that

The model definition and syntax is always the same. No matter if it's a SQL or NoSQL model.

3. Customize your model

Let's adapt the model schema to the needs of our todo app. I think for the start we need a title, text (What to do)and a flag if we are done.

So change the schema to this:

schema = {
'title': { 'type': 'string', 'maxlength' : 35 },
'text' : { 'type': 'string' },
'done' : { "type": "boolean" }
}

Save the model file and we are done.


4. let's play with the model

Now let's do something with the model we just created. We can use it right away. There is nothing else to do.

So open a python prompt in your todo virtual environment. (make sure you have the todo app directory in your PYTHONPATH)

And follow this asciinema cast to get an impression. 

(Best way is to watch it once and then do it yourself side by side...)

Next steps:

Continue the getting started path and add a handler and a REST API to your todo app