On Sat, Feb 20, 2021 at 12:57 PM Michael Merickel <
mmer...@gmail.com> wrote:
> Check out the new pattern of storing the request in the SQLAlchemy session object for easier access in your model layer without threadlocals!
Where is that? I don't see it in 'pyramid-cookiecutter-starter' or the 2.0 docs.
I do essentially the opposite. I have a request subclass with reified
methods for external resources like 'request.sa_session()'.
Cookiecutter is still using this concept although using
'config.add_request_method()' instead of a subclass:
https://github.com/Pylons/pyramid-cookiecutter-starter/blob/latest/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_name%7D%7D/sqlalchemy_models/__init__.py
(Lines 71-75.)
My model classes used to have querying methods but I moved away from
that because they ended up putting too much into the models and being
too limiting (my querying needs inevitably expanded beyond the method
arguments). I now have just the columns and relations in the models,
and separate lib modules for high-level queries. These take a session
argument. I don't usually need a request argument except occasionally
for URL generation. For that I have a postprocessing function that
adds the URL attributes to the query results. For instance:
# View
results = myapp.lib.mysearch.do_search(sa_session, ...criteria..)
# Return list of ORM objects,
postprocess(results, request)
# Iterate through objects, adding URL attributes for HTML links,
# formatting dates, calculating display values, etc.