Mixin classes for obtaining object of an SQLAlchemy model.
Note
The following packages must be installed:
Bases: flask_velox.mixins.context.ContextMixin
Mixin provides SQLAlchemy model integration.
SQLAlchemy un-instanciated model class
SQLAlchemy session instance
The primary key field name, defaults to id
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 |
---|
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 |
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 |
---|
Overrides set_context to set extra context variables.
See also
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:
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
|
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 |
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 |
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 |