flask.ext.admin.contrib.peewee

Peewee model backend implementation.

class ModelView(model, name=None, category=None, endpoint=None, url=None, static_folder=None, menu_class_name=None, menu_icon_type=None, menu_icon_value=None)

Class inherits configuration options from BaseModelView and they’re not displayed here.

column_filters = None

Collection of the column filters.

Can contain either field names or instances of flask.ext.admin.contrib.peewee.filters.BaseFilter classes.

For example:

class MyModelView(BaseModelView):
    column_filters = ('user', 'email')

or:

class MyModelView(BaseModelView):
    column_filters = (BooleanEqualFilter(User.name, 'Name'))
filter_converter = <flask_admin.contrib.peewee.filters.FilterConverter object at 0x7fc8259e0110>

Field to filter converter.

Override this attribute to use non-default converter.

model_form_converter = <class 'flask_admin.contrib.peewee.form.CustomModelConverter'>

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_form_converter = <class 'flask_admin.contrib.peewee.form.InlineModelConverter'>

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 = TextField('value')
        return form_class

class MyAdminView(ModelView):
    inline_model_form_converter = MyInlineModelConverter
fast_mass_delete = False

If set to False and user deletes more than one model using actions, all models will be read from the database and then deleted one by one giving Peewee chance to manually cleanup any dependencies (many-to-many relationships, etc).

If set to True, will run DELETE statement which is somewhat faster, but might leave corrupted data if you forget to configure DELETE CASCADE for your model.

inline_models = None

Inline related-model editing for models with parent to child relation.

Accept enumerable with one of the values:

  1. Child model class:

    class MyModelView(ModelView):
        inline_models = (Post,)
    
  2. Child model class and additional options:

    class MyModelView(ModelView):
        inline_models = [(Post, dict(form_columns=['title']))]
    
  3. Django-like InlineFormAdmin class instance:

    class MyInlineModelForm(InlineFormAdmin):
        form_columns = ('title', 'date')
    
    class MyModelView(ModelView):
        inline_models = (MyInlineModelForm(MyInlineModel),)
    

You can customize generated field name by:

  1. Using form_name property as option:

    class MyModelView(ModelView):

    inline_models = ((Post, dict(form_label=’Hello’)))

  2. Using field’s related_name:

    class Model1(Base):

    # ... pass

    class Model2(Base):

    # ... model1 = ForeignKeyField(related_name=”model_twos”)

    class MyModel1View(Base):

    inline_models = (Model2,) column_labels = {‘model_ones’: ‘Hello’}

action_view(*args, **kwargs)

Mass-model action view.

after_model_change(form, model, is_created)

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:
  • form – Form used to create/update model
  • model – Model that was created/updated
  • is_created – True if model was created, False if model was updated
create_blueprint(admin)

Create Flask blueprint.

create_form(obj=None)

Instantiate model creation form and return it.

Override to implement custom behavior.

create_view(*args, **kwargs)

Create model view

delete_view(*args, **kwargs)

Delete model view. Only POST method is allowed.

edit_form(obj=None)

Instantiate model editing form and return it.

Override to implement custom behavior.

edit_view(*args, **kwargs)

Edit model view

get_actions_list()

Return a list and a dictionary of allowed actions.

get_column_name(field)

Return a human-readable column name.

Parameters:field – Model field name.
get_create_form()

Create form class for model creation view.

Override to implement customized behavior.

get_edit_form()

Create form class for model editing view.

Override to implement customized behavior.

get_filter_arg(index, flt)

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:
  • index – Filter index in _filters array
  • flt – Filter instance
get_filters()

Return a list of filter objects.

If your model backend implementation does not support filters, override this method and return None.

get_form()

Get form class.

If self.form is set, will return it and will call self.scaffold_form otherwise.

Override to implement customized behavior.

get_list_columns()

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.

get_list_value(context, model, name)

Returns the value to be displayed in the list view

Parameters:
  • contextjinja2.runtime.Context
  • model – Model instance
  • name – Field name
get_sortable_columns()

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.

get_url(endpoint, **kwargs)

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:
  • endpoint – Flask endpoint name
  • kwargs – Arguments for url_for
handle_action(return_view=None)

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_filter(filter)

Postprocess (add joins, etc) for a filter.

Parameters:filter – Filter object to postprocess
inaccessible_callback(name, **kwargs)

Handle the response to inaccessible views.

By default, it throw HTTP 403 error. Override this method to customize the behaviour.

index_view(*args, **kwargs)

List view

init_actions()

Initialize list of actions for the current administrative view.

is_accessible()

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.

is_sortable(name)

Verify if column is sortable.

Parameters:name – Column name.
is_visible()

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.

on_form_prefill(form, id)

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:
  • form – Form instance
  • id – id of the object that is going to be edited
on_model_change(form, model, is_created)

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:
  • form – Form used to create/update model
  • model – Model that will be created/updated
  • is_created – Will be set to True if model was created and to False if edited
on_model_delete(model)

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, **kwargs)

Render template

Parameters:
  • template – Template path to render
  • kwargs – Template arguments
validate_form(form)

Validate the form on submit.

Parameters:form – Form to validate

Related Topics

This Page