flask_velox.mixins.sqla.read

Module provides mixin classes for dealing with SQLALchemy models

Note

The following packages must be installed:

  • Flask SQLAlchemy

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
class flask_velox.mixins.sqla.read.ListModelMixin(*args, **kwargs)

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')
base_query object, optional

A SQLAlchemy base query object, if defined this will be used instead of self.model.query.all()

objects_context_name str, optional

Instead of using object you can set the context variable name to use for the object, defaults to object

paginate bool, optional

Paginate the records using SQLAlchemy query.paginate, defaults True

per_page int, optional

If paginate is True customise the number of records to show per page, defaults to 30

get_basequery()

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
get_objects()

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
get_objects_context_name()

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
get_page()

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.

get_per_page()

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
set_context()

Adds extra context to SQLAlchemy based list views.

See also

  • from flask_velox.mixins.context.ContextMixin.set_context()

Note

Adds the following context variables.

  • objects: List of model objects
  • pagination: Pagination object or None
Returns:Rendered template
Return type:str
class flask_velox.mixins.sqla.read.ObjectMixin(*args, **kwargs)

Bases: flask_velox.mixins.sqla.object.SingleObjectMixin

Mixin for returning a single object.

get_object_context_name()

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_context()

Set the context for a Object view.

See also

  • from flask_velox.mixins.context.ContextMixin.set_context()

Note

Adds the following context variables.

  • object: List of columns
class flask_velox.mixins.sqla.read.TableModelMixin(*args, **kwargs)

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']
columns list

A list of columns to render, this should map to model fields

formatters dict

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
}
column_name(name)

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_value(field, instance)

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:
  • field (str) – Field name on model
  • instance (obj) – Instance of model
Returns:

Formatted value

Return type:

anything

get_columns()

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
get_formatters()

Return formatters defined for the View using this Mixin.

Returns:Returns defined formatters or None
Return type:dict or None
set_context()

Adds extra context to SQLAlchemy table based list views.

See also

  • from flask_velox.mixins.context.ContextMixin.set_context()

Note

Adds the following context variables.

  • columns: List of columns
  • column_name: column_name function
  • format_value: format_value function

http://thisissoon.com

Related Topics

This Page