Module provides mixin classes for dealing with SQLALchemy models
Note
The following packages must be installed:
Example
1 2 3 4 5 6 7 8 9 | from flask.ext.velox.mixins.sqla.model import ModelMixin
from yourapp import db
from yourapp.models import SomeModel
app = Flask(__name__)
class MyView(ModelMixin):
session = db.session
model = SomeModel
|
Bases: flask_velox.mixins.sqla.object.BaseModelMixin
Mixin provides ability to list multiple instances of a SQLAlchemy model.
As well as attributes supported by the classes this mixin inherits from other attributes are supported as well.
Example
1 2 3 4 5 6 | from flask.ext.velox.mixins.sqla.model import ListModelMixin
from yourapp.models import SomeModel
class MyView(ListModelMixin):
model = SomeModel
base_query = SomeModel.query.filter(foo='bar')
|
A SQLAlchemy base query object, if defined this will be used instead of self.model.query.all()
Instead of using object you can set the context variable name to use for the object, defaults to object
Paginate the records using SQLAlchemy query.paginate, defaults True
If paginate is True customise the number of records to show per page, defaults to 30
Returns SQLAlchemy base query object instance, if base_query is declared this will be used as the base query, else self.model.query.all() will be used which would get all model objects.
Returns: | A flask BaseQuery object instance |
---|---|
Return type: | flask_sqlalchemy.BaseQuery |
Returns a list of objects and pagination object if paginate is True, else None will be returned when paginate is not set or False.
Returns: | List of model object instances, Pagination object or None |
---|---|
Return type: | tuple |
Returns the context name to use when returning the objects to the template, defaults to objects.
Returns: | Name to use for context variable |
---|---|
Return type: | str |
Attempt to get the current page number, assumes a HTTP GET query param called page is availible in flask.request.args which holds the page number.
Returns the number of records to show per page for paginated result sets. Defaults to 30
Returns: | Number of records per page |
---|---|
Return type: | int |
Adds extra context to SQLAlchemy based list views.
See also
Note
Adds the following context variables.
Returns: | Rendered template |
---|---|
Return type: | str |
Bases: flask_velox.mixins.sqla.object.SingleObjectMixin
Mixin for returning a single object.
Returns the context name to use for returning the object to the template, defaults to object.
Returns: | Context name to use for object in template |
---|---|
Return type: | str |
Set the context for a Object view.
See also
Note
Adds the following context variables.
Bases: flask_velox.mixins.sqla.read.ListModelMixin
Mixin extends the default ListModelMixin behaviour adding attributes for rendering lists in tables.
Example
1 2 3 4 5 6 | from flask.ext.velox.mixins.sqla.model import TableModelMixin
from yourapp.models import SomeModel
class MyView(TableModelMixin):
model = SomeModel
columns = ['field1', 'field2', 'field3']
|
A list of columns to render, this should map to model fields
A dict of key value pairs mapping a field name (key) to a method which formats the fields data, for example:
formatters = {
'field1': fmt_bool
'field1': fmt_datetime
}
Attempts to get a human friendly name for the column. First it will look for an info attribute on the model field, if present will then return label value of the info dict. If not present this method will replace underscore characters with spaces and simply title case the individual words.
An example model with an info attribute, this is the same behaviour as WTForms-Alchemy:
class MyModel(db.Model):
field = db.Column(db.Unicode, info={
'label' = 'My Field'
})
Parameters: | name (str) – The column name mapping to a model field attribute |
---|---|
Returns: | Human friendly field name |
Return type: | str |
Format a given field name and instance with defined formatter if a formatter is defined for the specific field. This method is added to the context for use in a template, for example:
{% for object in objects %}
{% for column in columns %}
{{ format_value(column, object) }}
{% endfor %}
{% endfor %}
Parameters: |
|
---|---|
Returns: | Formatted value |
Return type: | anything |
Returns the list of columns defined for the View using this Mixin.
Returns: | List of strings of model field names |
---|---|
Return type: | list |
Raises: | NotImplementedError – If columns is not defined |
Return formatters defined for the View using this Mixin.
Returns: | Returns defined formatters or None |
---|---|
Return type: | dict or None |
Adds extra context to SQLAlchemy table based list views.
See also
Note
Adds the following context variables.