Where to put automatic setup_schema for ColanderAlchemy+Pyramid

107 views Asked by At

The docs say to put this somewhere:

from sqlalchemy import event
from colanderalchemy import setup_schema
event.listen(mapper, 'mapper_configured', setup_schema)

Where should this go in Pyramid? Should I be using Pyramid events instead of SQLAlchemy's?

When I tried putting it at the top of the models.py file, it complained about mapper not existing; should I still be using that?

2

There are 2 answers

1
Antti Haapala -- Слава Україні On BEST ANSWER

You need to use the SQLAlchemy events as they tell what is happening inside the SQLAlchemy (they do not relate to the pyramid events at all).

The documentation for the ColanderAlchemy is confusing; what they call for a mapper here is your model class (it is not a mapper).

Thus in your models you should be doing something like:

class MyModelClass(Base):
    ...

event.listen(
    MyModelClass,
    "mapper_configured",
    setup_schema)
2
Tim Tisdall On

The test suite shows it working like this:

from sqlalchemy import event
from colanderalchemy import setup_schema
from sqlalchemy.orm import mapper

event.listen(mapper, 'mapper_configured', setup_schema)

Please let me know if that fixes it for you and I can go update the documentation accordingly.