Reading SQLAlchemy models in views is simple.
The ModelListView allows you to render templates with paginated (or not) lists of model objects.
from flask.ext.velox.views.sqla import read
from yourapp.models import Model
class MyView(read.ModelListView):
model = Model
template = 'list.html'
The context returned to the template is as follows:
Flask-SQLAlchemy gives us pagination if we want it, by default paginate is True and the number of records is limited to 30, but this can all be changed and configured.
class MyView(read.ModelListView):
model = Model
template = 'list.html'
paginate = False
class MyView(read.ModelListView):
model = Model
template = 'list.html'
per_page = 10
Here are some example templates.
<ul>
{% for object in objects %}
<li>{{ object.name }}</li>
{% endfor %}
</ul>
<ul>
{% for object in objects %}
<li>{{ object.name }}</li>
{% endfor %}
</ul>
{% if objects.has_prev %}<a href="{{ url_for('rule', page=objects.prev_num) }}"><< Newer Objects{% else %}<< Newer Objects{% endif %} |
{% if objects.has_next %}<a href="{{ url_for('rule', page=objects.next_num) }}">Older Objects >></a>{% else %}Older Objects >>{% endif %}
If you do not wish objects to be used as the context name for your object list you can change this be defining a objects_context_name attribute on the class, for example:
from flask.ext.velox.views.sqla import read
from yourapp.models import Model
class MyView(read.ModelListView):
model = Model
objects_context_name = 'foos'
template = 'list.html'
In this case foos will be used instead of objects in the template.
The table model view is almost exactly the same as ModelListView so we won’t tread the same ground.
This view allows us to specify the columns we want to render in our table which relate to field names in the model:
from flask.ext.velox.views.sqla import read
from yourapp.models import Model
class MyView(read.ModelTableView):
model = Model
template = 'list.html'
columns = ['field1', 'field2', 'field3']
paginate = False
The context returned to the template is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <table>
<thead>
<tr>
{% for column in columns %}
<th>{{ column_name(column) }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for object in objects %}
<tr>
{% for column in columns %}
<tr>{{ format_value(column, object) }}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
|
We won’t go into detail about how column_name and format_value work here but you can check the API Reference for more details:
Pagination operates exactly the same as ListModelMixin.
The ObjectView functions like the list views above however simply returns a single object to the template. Please read the SingleObjectMixin documentation which explains how an object is retreived.
from flask.ext.velox.views.sqla import read
from yourapp.models import Model
class MyView(read.ObjectView):
model = Model
template = 'detail.html'
The context returned to the template is as follows:
<html>
<head>
<meta charset="utf-8" />
<title>{{ object.name }}</title>
</head>
<body>
{{ object.description }}
</body>
</html>
As with the list views you can change the context name used for accessing the object in the template by setting an object_context_name attribute on the view class:
from flask.ext.velox.views.sqla import read
from yourapp.models import Model
class MyView(read.ObjectView):
model = Model
template = 'detail.html'
object_context_name = 'foo'
In the above view foo will be returned to the template for accessing the object rather than object.