10 min read Author: klaas

Working with models (NoSQL)

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 this is super easy. All data you manage is organized in models. Let's assume we want to create the worlds best Todo list app. So we need to store todos. This is how we manage this in PythonOnWheels.

Aditionally PythonOnWheels data models directly support many SQL (Using sqlalchemy under the hood) and NoSQL DBs (tinyDB, mongodb) . 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. This in not only some dictionary like definition style, actually PythonOnWheels uses cerberus schema definitions for models. So we have schema validation directly onboard on the python side and it integrated the great cerberus library, so it's really reliable.

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)

Follow this asciinema cast to get an impression.

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