Hey guys, I'm developing a little project using SQLAlchemy and other tools, you can find code and docs here for context condor-ir.co, I'm working on a CLI and soon will be working on an API, the ORM side of things leaves me with a couple of models that look like this,
class MyObject(DeclarativeBase):
__tablename__ = 'my_object'
some_field = Column(SomeFieldClass, options...)
other_field = ...
those are my working models, and SQLAlchemy allows me to add custom methods and other stuff, however, I end up making stuff like this in my CLI code,
db = session()
if target is None:
bibset = db.query(BibliographySet).order_by('created desc').first()
else:
query = db.query(BibliographySet)
query = query.filter(BibliographySet.eid.like(target + '%'))
bibset = query.first()
if bibset is None:
click.echo('Please create a bibset first')
sys.exit(1)
That bothers me a little because a lot of the interaction with the database that could be just a method in a store class that manages my db session, and ads a great deal of boilerplate to my code, specially when creating a couple of related objects.
My question is, when using an ORM do you guys mind flooding your model classes with a bunch of class methods? Do you write your own manager objects? Or do you use the ORM right inside your code?
No responses yet.