SQLAlchemy model backend implementation.
SQLAlchemy model view
Usage sample:
admin = Admin()
admin.add_view(ModelView(User, db.session))
Class inherits configuration options from BaseModelView and they’re not displayed here.
Enable automatic detection of displayed foreign keys in this view and perform automatic joined loading for related models to improve query performance.
Please note that detection is not recursive: if __unicode__ method of related model uses another model to generate string representation, it will still make separate database call.
List of parameters for SQLAlchemy subqueryload. Overrides column_auto_select_related property.
For example:
class PostAdmin(ModelView):
column_select_related_list = ('user', 'city')
You can also use properties:
class PostAdmin(ModelView):
column_select_related_list = (Post.user, Post.city)
Please refer to the subqueryload on list of possible values.
Collection of the searchable columns. Only text-based columns are searchable (String, Unicode, Text, UnicodeText).
Example:
class MyModelView(ModelView):
column_searchable_list = ('name', 'email')
You can also pass columns:
class MyModelView(ModelView):
column_searchable_list = (User.name, User.email)
The following search rules apply:
Collection of the column filters.
Can contain either field names or instances of flask.ext.admin.contrib.sqla.filters.BaseFilter classes.
For example:
class MyModelView(BaseModelView):
column_filters = ('user', 'email')
or:
class MyModelView(BaseModelView):
column_filters = (BooleanEqualFilter(User.name, 'Name'))
Field to filter converter.
Override this attribute to use non-default converter.
Model form conversion class. Use this to implement custom field conversion logic.
For example:
class MyModelConverter(AdminModelConverter):
pass
class MyAdminView(ModelView):
model_form_converter = MyModelConverter
Inline model conversion class. If you need some kind of post-processing for inline forms, you can customize behavior by doing something like this:
class MyInlineModelConverter(AdminModelConverter):
def post_process(self, form_class, info):
form_class.value = wtf.StringField('value')
return form_class
class MyAdminView(ModelView):
inline_model_form_converter = MyInlineModelConverter
If set to False and user deletes more than one model using built in action, all models will be read from the database and then deleted one by one giving SQLAlchemy a chance to manually cleanup any dependencies (many-to-many relationships, etc).
If set to True, will run a DELETE statement which is somewhat faster, but may leave corrupted data if you forget to configure DELETE CASCADE for your model.
Inline related-model editing for models with parent-child relations.
Accepts enumerable with one of the following possible values:
Child model class:
class MyModelView(ModelView):
inline_models = (Post,)
Child model class and additional options:
class MyModelView(ModelView):
inline_models = [(Post, dict(form_columns=['title']))]
Django-like InlineFormAdmin class instance:
class MyInlineModelForm(InlineFormAdmin):
form_columns = ('title', 'date')
class MyModelView(ModelView):
inline_models = (MyInlineModelForm(MyInlineModel),)
You can customize the generated field name by:
Using the form_name property as a key to the options dictionary:
- class MyModelView(ModelView):
inline_models = ((Post, dict(form_label=’Hello’)))
Using forward relation name and column_labels property:
- class Model1(Base):
pass
- class Model2(Base):
# ... model1 = relation(Model1, backref=’models’)
- class MyModel1View(Base):
inline_models = (Model2,) column_labels = {‘models’: ‘Hello’}
Map choices to form fields
Example:
class MyModelView(BaseModelView):
form_choices = {'my_form_field': [
('db_value', 'display_value'),
]
List of field types that should be optional if column is not nullable.
Example:
class MyModelView(BaseModelView):
form_optional_types = (Boolean, Unicode)
Mass-model action view.
Perform some actions after a model was created or updated and committed to the database.
Called from create_model after successful database commit.
By default does nothing.
Parameters: |
|
---|
Controls if list view should display all relations, not only many-to-one.
Create Flask blueprint.
Instantiate model creation form and return it.
Override to implement custom behavior.
Create model from form.
Parameters: | form – Form instance |
---|
Create model view
Delete model.
Parameters: | model – Model to delete |
---|
Delete model view. Only POST method is allowed.
Instantiate model editing form and return it.
Override to implement custom behavior.
Edit model view
Return a list and a dictionary of allowed actions.
Return a human-readable column name.
Parameters: | field – Model field name. |
---|
Return a the count query for the model type
A query(self.model).count() approach produces an excessive subquery, so query(func.count(‘*’)) should be used instead.
See #45a2723 commit message for details.
Create form class for model creation view.
Override to implement customized behavior.
Create form class for model editing view.
Override to implement customized behavior.
Given a filter flt, return a unique name for that filter in this view.
Does not include the flt[n]_ portion of the filter name.
Parameters: |
|
---|
Return a list of filter objects.
If your model backend implementation does not support filters, override this method and return None.
Get form class.
If self.form is set, will return it and will call self.scaffold_form otherwise.
Override to implement customized behavior.
Return models from the database.
Parameters: |
|
---|
Returns a list of the model field names. If column_list was set, returns it. Otherwise calls scaffold_list_columns to generate the list from the model.
Returns the value to be displayed in the list view
Parameters: |
|
---|
Return a single model by its id.
Parameters: | id – Model id |
---|
Return the primary key value from a model object. If there are multiple primary keys, they’re encoded into string representation.
Return a query for the model type.
If you override this method, don’t forget to override get_count_query as well.
Returns a dictionary of the sortable columns. Key is a model field name and value is sort column (for example - attribute).
If column_sortable_list is set, will use it. Otherwise, will call scaffold_sortable_columns to get them from the model.
Generate URL for the endpoint. If you want to customize URL generation logic (persist some query string argument, for example), this is right place to do it.
Parameters: |
|
---|
Handle action request.
Parameters: | return_view – Name of the view to return to after the request. If not provided, will return user to the index view. |
---|
Handle the response to inaccessible views.
By default, it throw HTTP 403 error. Override this method to customize the behaviour.
List view
Initialize list of actions for the current administrative view.
Initialize search. Returns True if search is supported for this view.
For SQLAlchemy, this will initialize internal fields: list of column objects used for filtering, etc.
Override this method to add permission checks.
Flask-Admin does not make any assumptions about the authentication system used in your application, so it is up to you to implement it.
By default, it will allow access for everyone.
Verify if column is sortable.
Parameters: | name – Column name. |
---|
Verify if the provided column type is text-based.
Returns: | True for String, Unicode, Text, UnicodeText, varchar |
---|
Verify that the provided filter object is derived from the SQLAlchemy-compatible filter class.
Parameters: | filter – Filter object to verify. |
---|
Override this method if you want dynamically hide or show administrative views from Flask-Admin menu structure
By default, item is visible in menu.
Please note that item should be both visible and accessible to be displayed in menu.
Perform additional actions to pre-fill the edit form.
Called from edit_view, if the current action is rendering the form rather than receiving client side input, after default pre-filling has been performed.
By default does nothing.
You only need to override this if you have added custom fields that depend on the database contents in a way that Flask-admin can’t figure out by itself. Fields that were added by name of a normal column or relationship should work out of the box.
Parameters: |
|
---|
Perform some actions after a model is created or updated.
Called from create_model and update_model in the same transaction (if it has any meaning for a store backend).
By default does nothing.
Parameters: |
|
---|
Perform some actions before a model is deleted.
Called from delete_model in the same transaction (if it has any meaning for a store backend).
By default do nothing.
Render template
Parameters: |
|
---|
Return a list of joined tables by going through the displayed columns.
Return list of enabled filters
Create form from the model.
Contribute inline models to the form
Parameters: | form_class – Form class |
---|
Return a list of columns from the model.
Return the primary key name(s) from a model If model has single primary key, will return a string and tuple otherwise
Return a dictionary of sortable columns. Key is column name, value is sort column/field.
Update model from form.
Parameters: |
|
---|
Validate the form on submit.
Parameters: | form – Form to validate |
---|