API

Core

class flask_sqlalchemy_booster.core.FlaskSQLAlchemyBooster(**kwargs)[source]

Bases: flask_sqlalchemy.SQLAlchemy

Sets the Model class to ModelBooster, providing all the methods defined on ModelBooster

Examples

>>> db = FlaskSQLAlchemyBooster()
>>> class User(db.Model):
        id = db.Column(db.Integer, primary_key=True, unique=True)
        email = db.Column(db.String(100), unique=True)
        password = db.Column(db.String(100))
        name = db.Column(db.String(100))
        contact_number = db.Column(db.String(20))
>>> User.all()
>>> u = User.first()
>>> u.todict()
make_declarative_base()[source]
class flask_sqlalchemy_booster.core.QueryPropertyWithModelClass(sa)[source]

Bases: flask_sqlalchemy._QueryProperty

Subclassed to add the cls attribute to a query instance.

This is useful in instances when we need to find the class of the model being queried when provided only with a query object

Utils

flask_sqlalchemy_booster.utils.is_dict_like(rel_instance)[source]
flask_sqlalchemy_booster.utils.is_list_like(rel_instance)[source]

JSON Encoder

flask_sqlalchemy_booster.json_encoder.json_encoder(obj)[source]

Dictizable Mixin

DictizableMixin A mixin class to add todict method to objects.

class flask_sqlalchemy_booster.dictizable_mixin.DictizableMixin[source]

Bases: object

_attrs_to_serialize_

list of str

The columns which should be serialized as a part of the output dictionary

_key_modifications_

dict of str,str

A dictionary used to map the display names of columns whose original name you want to be modified in the json

_rels_to_serialize_

list of (str, str)

A list of tuples. The first element of the tuple is the relationship that is to be serialized. The second element it the name of the attribute in the related model, the value of which is to be used as the representation

_rels_to_expand_

list of str

A list of relationships to expand. You can specify nested relationships by placing dots.

_group_listrels_by_

dict of str, list of str

A dictionary representing how to hierarchially group a list like relationship. The relationship fields are the keys and the list of the attributes based on which they are to be grouped are the values.

classmethod is_list_attribute(rel)[source]
serialize_attrs(*args)[source]

Converts and instance to a dictionary with only the specified attributes as keys

Parameters:*args

The arguments to serialize

Examples

>>> customer = Customer.create(name="James Bond", email="007@mi.com",
                               phone="007", city="London")
>>> customer.serialize_attrs('name', 'email')
{'name': u'James Bond', 'email': u'007@mi.com'}
to_serializable_dict(attrs_to_serialize=None, rels_to_expand=None, rels_to_serialize=None, key_modifications=None)[source]

Just an alias for some functions which might still use old name

todict(attrs_to_serialize=None, rels_to_expand=None, rels_to_serialize=None, group_listrels_by=None, key_modifications=None)[source]

Converts an instance to a dictionary form

Parameters:
  • attrs_to_serialize (list of str) – The columns which should be serialized as a part of the output dictionary
  • key_modifications (dict of str,str) – A dictionary used to map the display names of columns whose original name you want to be modified in the json
  • rels_to_serialize (list of (str, str)) – A list of tuples. The first element of the tuple is the relationship that is to be serialized. The second element it the name of the attribute in the related model, the value of which is to be used as the representation
  • rels_to_expand (list of str) – A list of relationships to expand. You can specify nested relationships by placing dots.
  • group_listrels_by (dict of str, list of str) – A dictionary representing how to hierarchially group a list like relationship. The relationship fields are the keys and the list of the attributes based on which they are to be grouped are the values.
tojson(attrs_to_serialize=None, rels_to_expand=None, rels_to_serialize=None, key_modifications=None)[source]
flask_sqlalchemy_booster.dictizable_mixin.serialized_list(olist, rels_to_expand=[])[source]

Queryable Mixin

Borrows heavily from https://github.com/mattupstate/overholt/blob/master/overholt/core.py

class flask_sqlalchemy_booster.queryable_mixin.QueryableMixin[source]

Bases: object

_no_overwrite_

list

The list of attributes that should not be overwritten

classmethod add(model, commit=True)[source]
classmethod add_all(models, commit=True, check_type=False)[source]
classmethod all(*criterion, **kwargs)[source]

Returns a list of instances of the service’s model filtered by the specified key word arguments.

Parameters:**kwargs

filter parameters

classmethod build(**kwargs)[source]

Returns a new, added but unsaved instance of the service’s model class.

Parameters:**kwargs

instance parameters

classmethod build_all(list_of_kwargs)[source]
commit()[source]
classmethod count(*criterion, **kwargs)[source]
classmethod create(**kwargs)[source]

Returns a new, saved instance of the service’s model class.

Parameters:**kwargs

instance parameters

classmethod create_all(list_of_kwargs)[source]
delete(commit=True)[source]
classmethod filter(*criterion, **kwargs)[source]
classmethod filter_by(**kwargs)[source]

Returns a list of instances of the model filtered by the specified key word arguments.

Parameters:**kwargs
classmethod find_or_build(**kwargs)[source]

Checks if an instance already exists in db with these kwargs else returns a new, saved instance of the service’s model class.

Parameters:**kwargs

instance parameters

classmethod find_or_build_all(list_of_kwargs)[source]
classmethod find_or_create(**kwargs)[source]

Checks if an instance already exists in db with these kwargs else returns a new, saved instance of the service’s model class.

Parameters:**kwargs

instance parameters

classmethod find_or_create_all(list_of_kwargs, keys=[])[source]
classmethod first(*criterion, **kwargs)[source]

Returns the first instance found of the service’s model filtered by the specified key word arguments.

Parameters:**kwargs

filter parameters

classmethod get(keyval, key='id', user_id=None)[source]

Returns an instance of the service’s model with the specified id. Returns None if an instance with the specified id does not exist.

Parameters:id – the instance id
classmethod get_all(keyvals, key='id', user_id=None)[source]
classmethod get_and_setattr(id, **kwargs)[source]

Returns an updated instance of the service’s model class.

Parameters:
  • model – the model to update
  • **kwargs

    update parameters

classmethod get_and_update(id, **kwargs)[source]

Returns an updated instance of the service’s model class.

Parameters:
  • model – the model to update
  • **kwargs

    update parameters

classmethod get_or_404(id)[source]

Returns an instance of the service’s model with the specified id or raises an 404 error if an instance with the specified id does not exist

Parameters:id – the instance id
classmethod last(*criterion, **kwargs)[source]
classmethod new(**kwargs)[source]

Returns a new, unsaved instance of the service’s model class.

Parameters:**kwargs

instance parameters

classmethod new_all(list_of_kwargs)[source]
classmethod one(*criterion, **kwargs)[source]
classmethod rollback_session()[source]
save()[source]

Saves a model instance to db

>>> customer = Customer.new(name="hari")
>>> customer.save()
update(**kwargs)[source]

updates an instance

Parameters:
  • **kwargs

    Column names are keywords and their new values are the values

  • customer.update(email=”newemail@x.com”, name=”new”) (>>>) –
classmethod update_all(*criterion, **kwargs)[source]
classmethod update_or_create(**kwargs)[source]
classmethod update_or_create_all(list_of_kwargs, keys=[])[source]

QueryBooster

class flask_sqlalchemy_booster.query_booster.QueryBooster(entities, session=None)[source]

Bases: flask_sqlalchemy.BaseQuery

asc(attr='id')[source]
cls = None
desc(attr='id')[source]

ModelBooster

class flask_sqlalchemy_booster.model_booster.ModelBooster[source]

Bases: flask_sqlalchemy.Model, flask_sqlalchemy_booster.queryable_mixin.QueryableMixin, flask_sqlalchemy_booster.dictizable_mixin.DictizableMixin

query_class

alias of QueryBooster

session = None

Responses

flask_sqlalchemy_booster.responses.appropriate_json(olist, **kwargs)[source]
flask_sqlalchemy_booster.responses.as_json(struct, status=200, wrap=True, meta=None)[source]
flask_sqlalchemy_booster.responses.as_json_list(olist, attrs_to_serialize=None, rels_to_expand=None, rels_to_serialize=None, group_listrels_by=None, key_modifications=None, groupby=None, keyvals_to_merge=None, meta=None)[source]
flask_sqlalchemy_booster.responses.as_json_obj(o, attrs_to_serialize=None, rels_to_expand=None, rels_to_serialize=None, group_listrels_by=None, key_modifications=None, groupkeys=None, meta=None)[source]
flask_sqlalchemy_booster.responses.as_list(func)[source]
flask_sqlalchemy_booster.responses.as_list_or_obj(func)[source]
flask_sqlalchemy_booster.responses.as_obj(func)[source]
flask_sqlalchemy_booster.responses.as_processed_list(func)[source]
flask_sqlalchemy_booster.responses.filter_query_with_key(query, keyword, value, op)[source]
flask_sqlalchemy_booster.responses.jsoned(struct, wrap=True, meta=None, wrap_key='result')[source]
flask_sqlalchemy_booster.responses.jsoned_list(olist, **kwargs)[source]
flask_sqlalchemy_booster.responses.jsoned_obj(obj, **kwargs)[source]
flask_sqlalchemy_booster.responses.serializable_list(olist, attrs_to_serialize=None, rels_to_expand=None, group_listrels_by=None, rels_to_serialize=None, key_modifications=None, groupby=None, keyvals_to_merge=None)[source]
flask_sqlalchemy_booster.responses.serializable_obj(obj, attrs_to_serialize=None, rels_to_expand=None, group_listrels_by=None, rels_to_serialize=None, key_modifications=None)[source]
flask_sqlalchemy_booster.responses.serialized_list(olist, **kwargs)[source]

Misnamed. Should be deprecated eventually.

flask_sqlalchemy_booster.responses.serialized_obj(obj, attrs_to_serialize=None, rels_to_expand=None, group_listrels_by=None, rels_to_serialize=None, key_modifications=None)[source]

Misnamed. Should be deprecated eventually.

Module contents

flask_sqlalchemy_booster

A wrapper over Flask-SQLAlchemy

Table Of Contents

This Page