API

flask.ext.admin.create_admin_blueprint(datastore, name='admin', list_view_pagination=25, view_decorator=None, empty_sequence=u'x1a', **kwargs)

Returns a Flask blueprint that provides the admin interface views. This blueprint will need to be registered to your flask application. The datastore parameter should be an set to be an instantiated AdminDatastore.

For example, typical usage would look something like this:

from flask import Flask
from flask.ext.admin.datastore.sqlalchemy import SQLAlchemyDatastore

from my_application import models, db_session

app = Flask(__name__)
datastore = SQLAlchemyDatastore(models, db_session)
admin = create_admin_blueprint(datastore)
app.register_blueprint(admin, url_prefix='/admin')

You can optionally specify the name to be used for your blueprint. The blueprint name preceeds the view names in the endpoints, if for example you want to refer to the views using flask.url_for.

Note

If you are using more than one admin blueprint from within the same app, it is necessary to give each admin blueprint a different name so the admin blueprints will have distinct endpoints.

The view_decorator parameter can be set to a decorator that will be applied to each admin view function. For example, you might want to set this to a decorator that handles authentication (e.g. login_required). See the authentication/view_decorator.py for an example of this.

The list_view_pagination parameter sets the number of items that will be listed per page in the list view.

Finally, the empty_sequence keyword can be used to designate a sequence of characters that can be used as a substitute for cases where part of the key url may be empty. This should be a rare occurance, but might comes up when using composite keys which can contain empty parts. By default, empty_sequence is set to %1A, the substitute control character.

Datastores

class flask.ext.admin.datastore.core.AdminDatastore

A base class for admin datastore objects. All datastores used in Flask-Admin should subclass this object and define the following methods.

create_model_pagination(model_name, page, per_page=25)

Returns a pagination object for the list view.

delete_model_instance(model_name, model_keys)

Deletes a model instance. Returns True if model instance was successfully deleted, returns False otherwise.

find_model_instance(model_name, model_keys)

Returns a model instance, if one exists, that matches model_name and model_keys. Returns None if no such model instance exists.

get_model_class(model_name)

Returns a model class, given a model name.

get_model_form(model_name)

Returns a form, given a model name.

get_model_keys(model_instance)

Returns the keys for a given a model instance. This should be an iterable (e.g. list or tuple) containing the keys.

list_model_names()

Returns a list of model names available in the datastore.

save_model(model_instance)

Persists a model instance to the datastore. Note: this could be called when a model instance is added or edited.

update_from_form(model_instance, form)

Returns a model instance whose values have been updated with the values from a given form.

class flask.ext.admin.datastore.sqlalchemy.SQLAlchemyDatastore(models, db_session, model_forms=None, exclude_pks=True)

A datastore class for accessing SQLAlchemy models.

The models parameter should be either a module or an iterable (like a tuple or a list) that contains the SQLAlchemy models that will be made available through the admin interface.

db_session should be the SQLAlchemy session that the datastore will use to access the database. The session should already be bound to an engine. See the SQLAlchemy in Flask documentation for more information on how to configure the session.

By default, a form for adding and editing data will be automatically generated for each SQLAlchemy model. You can also create custom forms if you need more control over what the forms look like or how they behave. To use custom forms, set the model_forms parameter to be a dict with model names as keys matched to custom forms for the forms you want to override. Forms should be WTForms form objects; see the WTForms documentation for more information on how to configure forms.

Finally, the exclude_pks parameter can be used to specify whether or not to automatically exclude fields representing the primary key in auto-generated forms. The default is True, so the generated forms will not expose the primary keys of your models. This is usually a good idea if you are using a primary key that doesn’t have any meaning outside of the database, like an auto-incrementing integer, because changing a primary key changes the nature of foreign key relationships. If you want to expose the primary key, set this to False.

class flask.ext.admin.datastore.mongoalchemy.MongoAlchemyDatastore(models, db_session, model_forms=None)

A datastore for accessing MongoAlchemy document models.

The models parameter should be either a module or an iterable that contains the MongoAlchemy models that will be made available through the admin interface.

db_session should be an initialized MongoAlchemy session object. See the MongoAlchemy documentation for information on how to do that.

By default, a form for adding and editing data will be automatically generated for each MongoAlchemy model. Only primitive MongoAlchemy types are supported so if you need to support other fields you will need to create custom forms. You can also use custom forms if you want more control over form behavior. To use custom forms, set the model_forms parameter to be a dict with model names as keys matched to custom forms for the forms you want to override. Forms should be WTForms form objects; see the WTForms documentation for more information on how to configure forms.

A dict with model names as keys, mapped to WTForm Form objects that should be used as forms for creating and editing instances of these models.

Fork me on GitHub