Skip to main content

Pony ORM integration in Morepath

Project description

CI Status

more.pony: Pony ORM integration in Morepath

This package provides Morepath integration for the Pony Object-Relational Mapper library.

This package binds the database session to the request so you can interact with the database in your App directly without using db_session.

Quick start

Install more.pony:

$ pip install -U more.pony

Extend your App class from PonyApp:

from more.pony import PonyApp

class App(PonyApp):

Create your model:

from pony.orm import Database, PrimaryKey, Optional

db = Database()

class Document(db.Entity):
    _table_ = 'document'

    id = PrimaryKey(int, auto=True)
    title = Optional(str)
    content = Optional(str)

    def update(self, payload={}):

    def remove(self):

Setup the database in your start script:

import morepath

from .app import App
from .model import db

def run():
    db.bind(provider='sqlite', filename='app.db', create_db=True)


Now you can use the model in your path:

from .app import App
from .model import Document

@App.path(model=Document, path='documents/{id}')
def get_document(request, id=0):
    return Document[id]

And in your view:

from .app import App
from .model import Document

def document_default(self, request):
    return {
        'title': self.title,
        'content': self.content,

@App.json(model=Document, request_method='PUT')
def document_update(self, request):

@App.json(model=Document, request_method='DELETE')
def document_remove(self, request):


You can set the arguments which are passed to db_session in the pony section of your settings.

The default settings are:

def get_pony_settings():
    return {
        'allowed_exceptions': [],
        'immediate': False,
        'retry': 0,
        'retry_exceptions': [TransactionError],
        'serializable': False,
        'strict': False

More information about the arguments you find in the Pony API documentation.

You can also use the database settings section for your database setup, which allows you to use different setups for production, development and testing environments.

Just create create an App for each environment and load specific settings files:

class App(PonyApp):

with open('settings/default.yml') as defaults:
  defaults_dict = yaml.load(defaults)


class ProductionApp(App):

with open('settings/production.yml') as settings:
    settings_dict = yaml.load(settings)


class TestApp(App):

with open('settings/test.yml') as settings:
    settings_dict = yaml.load(settings)


The database configuration in the YAML settings files, depending on the database you use, could look something like:

  provider: sqlite
  filename: app.db
  create_db: true

In your start script you setup the database and load the App according to the RUN_ENV environment variable:

import os
import morepath

from .app import App, ProductionApp, TestApp
from .model import db

def setup_db(app):
    db_params = app.settings.database.__dict__.copy()

def run():

  if os.getenv('RUN_ENV') == 'production':
      app = ProductionApp()
  elif os.getenv('RUN_ENV') == 'test':
      app = TestApp()
      app = App()


Detail about the database configuration you find in the PonyOrm documentation.

Side effects

If you want to trigger side effects (like sending an email or writing to filesystem) on database commits you can emit a signal in the @request.after of the view which triggers the side effects.

Like this the side effects will be triggered just before the database session gets committed and only if it wasn’t rolled back.

This example uses more.emit:

@App.json(model=Document, request_method='PUT')
def document_update(self, request):

    def after(response):'document_updated', self, request)

Altenatively you can use in your model the PonyORM after_insert(), after_update() or after_delete() entity-hooks. This makes sure that the side effect is triggered after the database session has committed.

The drawback is that you don’t have easy access to the request or app in the model.


0.4 (2024-03-03)

  • Removed: Drop support for Python 3.4, 3.5, 3.6 and 3.7.

  • Add support for Python 3.9, 3.10 and 3.11.

  • Fix Flake8.

  • Use GitHub Actions for CI.

  • Make Python 3.11 the default testing environment.

  • Upgrade PonyORM to 0.7.17.

  • Show full diffs in the test output.

  • Add Editors config and venv to .gitignore.

  • Remove report to coveralls.

  • Update pre-commit revs.

0.3 (2020-04-26)

  • Removed: Removed support for Python 2.

    You have to upgrade to Python 3 if you want to use this version.

  • Added support for Python 3.6, 3.7 and 3.8 and PyPy 3.6.

  • Make Python 3.7 the default testing environment.

  • Upgrade PonyORM to 0.7.13.

  • Add integration for the Black code formatter.

0.2 (2017-07-20)

  • Upgrade PonyORM to 0.7.2.

  • Use a dictonary for passing parameters to db.bind in examples and tests.

0.1 (2017-04-22)

  • Initial public release.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

more.pony-0.4.tar.gz (7.9 kB view hashes)

Uploaded Source

Built Distribution

more.pony-0.4-py2.py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page