Quickstart

This page gives a brief introduction to the module. It assumes you have the module installed, if you don’t check the Installing portion. You can find examples here.

Create a basic User model

Let’s create a basic User model without adding it to the database

The code will be something like this:

from postDB import Model, Column, types

class User(Model):
    id = Column(types.Integer(big=True), primary_key=True)
    username = Column(types.String)
    email = Column(types.String, unique=True)

if __name__ == "__main__":
    print(User.create_table_sql())

    user = User(id=5, username="frank", email="frank@doesnotexist")

    print(user.as_dict())
    print(user.as_dict("id", "username"))

Let’s understand this example:

  • We import Model, Column and postDB.types.

  • We create a User class that is subclassed from Model.

  • We define the columns by using Column. We provide the type and we can also use keyword arguments like primary_key or unique to customize the column.

  • Then, we print the SQL query for creating the table in the database.

  • We create a User with data given for each column.

  • We print the dictionary for the user with every column.

  • We print the dictionary for the user with only the id and username columns.