Can I use pony orm with models in separate files?

46 Views Asked by At

Is it possible to use Pony with a file structure like this?

-project/
    -start.py
    -controller/
    -view/
    -model/
        __init__.py
        -person.py
        -category.py

All examples I have seen were single file setups like this:

db = Database()

class Person(db.Entity):
    ...

class Category(db.Entity):
    ...

db.bind(...)
db.generate_mapping(create_tables=True)

I want to move the class declarations to the model folder.

1

There are 1 best solutions below

0
UndoingTech On

Thank goodness for the related questions section. It looks like this question has already been asked and answered: PonyORM - multiple model files. Thank you to Alexander Kozlovsky for providing a detailed answer to that question.

My summary of Alexander Kozlovsky's answer:

  1. Have a file somewhere
# database.py
from pony.orm import *

db = Database()
  1. In the model files, do from database import db.

  2. Wherever db.generate_mapping() is, import all your models at the top of that file.