flask_velox.mixins.sqla.object

Mixin classes for obtaining object of an SQLAlchemy model.

Note

The following packages must be installed:

  • Flask-SQLAlchemy
class flask_velox.mixins.sqla.object.BaseModelMixin(*args, **kwargs)

Bases: flask_velox.mixins.context.ContextMixin

Mixin provides SQLAlchemy model integration.

model class

SQLAlchemy un-instanciated model class

session object

SQLAlchemy session instance

pk_field str, optional

The primary key field name, defaults to id

get_model()

Returns the Model to perform queries against.

Example

>>> from flask.ext.velox.mixins.sqla.model import BaseModelMixin
>>> from yourapp.models import SomeModel
>>> class MyView(BaseModelMixin):
...     model = SomeModel
>>> view = MyView()
>>> view.get_model()
yourapp.models.SomeModel
Raises:NotImplementedError – If model has not been declared on the class
get_pk_field()

Returns the primary key field name. If pk_field is not declared return value defaults to id.

Example

>>> from flask.ext.velox.mixins.sqla.model import BaseModelMixin
>>> class MyView(BaseModelMixin):
...     pk_field = 'foo'
>>> view = MyView()
>>> view.get_pk_field()
'foo'
Returns:Primary key field name
Return type:str
get_session()

Returns the SQLAlchemy db session instance.

Example

>>> from flask.ext.velox.mixins.sqla.model import BaseModelMixin
>>> from yourapp import db
>>> class MyView(BaseModelMixin):
...     session = db.session
>>> view = MyView()
>>> view.get_session()
<sqlalchemy.orm.scoping.scoped_session at 0x104c88dd0>
Raises:NotImplementedError – If session has not been declared on the class
set_context()

Overrides set_context to set extra context variables.

See also

  • from flask_velox.mixins.context.ContextMixin.set_context()
class flask_velox.mixins.sqla.object.SingleObjectMixin(*args, **kwargs)

Bases: flask_velox.mixins.sqla.object.BaseModelMixin

Mixin handles retrieving a single object from an SQLAlchemy model. This is done by defining a field query.

The value to use in the query is obtained by trying different request attributes for the data:

  1. request.view_args - Data passed as part of a uri
  2. request.args - Data passed as part of aquery string
  3. self - An attribute defined on the view class

The field used for the lookup is also the name of the value passed in the request, for example if lookup_field is set to foo then request.view_args / request.args should have an element named foo containing the data or an attribute on the class named foo.

Examples

1
2
3
4
5
from flask.ext.velox.mixins.sqla.object import SingleObjectMixin
from yourapp.models import MyModel

class MyView(SingleObjectMixin):
    model = MyModel
get_lookup_field()

Returns the field to lookup objects against, if lookup_field is not defined id will be returned by default.

Returns:Field to use for lookup, defaults to id
Return type:str
get_lookup_value()

Attempt to get the value to use for looking up the object in the database, this is usually an id number but could technically be anything.

Returns:Value to use in the lookup query
Return type:anything
get_object()

Returns an object from the database or a blank object if no lookup value is provided.

Returns:Populated or blank model object
Return type:object

http://thisissoon.com

Related Topics

This Page