API Reference¶
-
class
chemist.managers.
Manager
(model_klass, context)[source]¶ -
-
find_by
(**kw)[source]¶ Find a list of models that could be found in the database and match all the given keyword-arguments
-
find_one_by
(**kw)[source]¶ Find a single model that could be found in the database and match all the given keyword-arguments
-
from_result_proxy
(proxy, result)[source]¶ Creates a new instance of the model given an instance of
sqlalchemy.engine.ResultProxy
-
generate_query
(order_by=None, limit_by=None, offset_by=None, **kw)[source]¶ Queries the table with the given keyword-args and optionally a single order_by field.
-
get_or_create
(**data)[source]¶ Tries to get a model from the database that would match the given keyword-args through
Manager.find_one_by()
. If not found, a new instance is created in the database throughManager.create()
-
-
class
chemist.orm.
Context
(default_uri=None)[source]¶ Context is a system component that keeps track of multiple engines, switch between them and manage their lifecycle.
It also provides a
sqlalchemy.MetaData
instance that is automatically bound toIts purpose is to leverage quicky swapping the engine between “unit” tests.
-
class
chemist.orm.
ORM
(name, bases, attrs)[source]¶ metaclass for
chemist.models.Model
-
class
chemist.models.
Model
(engine=None, **data)[source]¶ Super-class of active record models.
Example:
class BlogPost(Mode): table = db.Table( 'blog_post', metadata, db.Column('id', db.Integer, primary_key=True), db.Column('title', db.Unicode(200), nullable=False), db.Column('slug', db.Unicode(200), nullable=False), db.Column('content', db.UnicodeText, nullable=False), ) def preprocess(self, data): # always derive slug from title data['slug'] = slugify(data['title']) return data
-
delete
()[source]¶ Deletes the current model from the database (removes a row that has the given model primary key)
-
initialize
()[source]¶ Dummy method to be optionally overwritten in the subclasses. Gets automatically called once a model instance is constructed.
-
is_persisted
¶ boolean property that returns True if the primary key is set. This property does not perform I/O against the database
-
manager
¶ alias of
chemist.managers.Manager
-
post_delete
()[source]¶ called right after executing a deletion. This method can be overwritten by subclasses in order to take any domain-related action
-
post_save
(transaction)[source]¶ called right after executing a save. This method can be overwritten by subclasses in order to take any domain-related action
-
pre_delete
()[source]¶ called right before executing a deletion. This method can be overwritten by subclasses in order to take any domain-related action
-
pre_save
()[source]¶ called right before executing a save. This method can be overwritten by subclasses in order to take any domain-related action
-
preprocess
(data)[source]¶ Placeholder for your own custom preprocess method, remember it must return a dictionary.
class BlogPost(Mode): table = db.Table( 'blog_post', metadata, db.Column('id', db.Integer, primary_key=True), db.Column('title', db.Unicode(200), nullable=False), db.Column('slug', db.Unicode(200), nullable=False), db.Column('content', db.UnicodeText, nullable=False), ) def preprocess(self, data): # always derive slug from title data['slug'] = slugify(data['title']) return data
-
refresh
()[source]¶ updates the current record with fresh values retrieved by
find_one_by()
and also returns a brand new instance.Note
any unsaved changes in the model will be lost upon calling this method.
-
save
(input_engine=None)[source]¶ Persists the model instance in the DB. It takes care of checking whether it already exists and should be just updated or if a new record should be created.
-
serialize
()[source]¶ pre-serializes the model, returning a dictionary with key-values.
This method is use by the to_dict() and only exists as a separate method so that subclasses overwriting to_dict can call serialize() rather than super(SubclassName, self).to_dict()
-
to_dict
()[source]¶ pre-serializes the model, returning a dictionary with key-values.
This method can be overwritten by subclasses at will.
Example:
>>> post = BlogPost.create(title='Some Title', content='loren ipsum') >>> post.to_dict() { 'id': 1, 'title': 'Some Title', 'slug': 'some-title', }
-
to_insert_params
()[source]¶ utility method used internally to generate a dict with all the serialized values except primary keys.
Example:
>>> post = BlogPost.create(title='Some Title', content='loren ipsum') >>> post.to_insert_params() { 'title': 'Some Title', 'slug': 'some-title', }
-