Skip to main content

List page

Various options that allow configuration in list page are:
  • column_list: List of columns or column names to be displayed in the list page.
  • column_exclude_list: List of columns or column names to be excluded in the list page.
  • column_display_link: List of columns or column names that allow to navigate in edit page
  • column_formatters: Dictionary of column formatters in the list page.
  • column_searchable_list: List of columns or column names to be searchable in the list page.
  • column_sortable_list: List of columns or column names to be sortable in the list page.
  • column_default_sort: Default sorting if no sorting is applied, tuple of (column, is_descending) or list of the tuple for multiple columns.
  • column_filters: List of columns or column names to be filterable in the list page.
  • list_per_page: Number of items to be display in list page. Default value is 10.
  • list_query: A method to customize the list query.
  • count_query: A method to customize the count query.
  • search_query: A method to customize the search query.
  • details_query: A method to customize the details query.
class UserAdmin(ModelView):
    model = User
    column_list = [User.id, User.name]
    column_searchable_list = [User.name]
    column_display_link = [User.name]
    column_sortable_list = [User.id]
    column_formatters = {User.name: lambda m, a: m.name[:10]}
    column_default_sort = [(User.email, True), (User.name, False)]
    column_filters = [User.name]
    list_per_page = 20

You can use the special keyword __all__ in column_list if you don’t want to specify all the columns manually. For example: column_list = __all__

Forms

Form customization with your models. The forms are based on WTForms package and include the following options:
  • form: Default form to be used for creating or editing the model. Default value is None and form is created dynamically.
  • form_base_class: Default base class for creating forms. Default value is wtforms.Form.
  • form_args: Dictionary of form field arguments supported by WTForms.
  • form_widget_args: Dictionary of form widget rendering arguments supported by WTForms.
  • form_columns: List of model columns to be included in the form. Default is all model columns.
  • form_excluded_columns: List of model columns to be excluded from the form.
  • form_overrides: Dictionary of form fields to override when creating the form.
  • form_converter: Allow adding custom converters to support additional column types.
  • form_edit_query: A method to customize the edit form data.
  • form_rules: List of form rules to manage rendering and behaviour of form.
  • form_create_rules: List of form rules to manage rendering and behaviour of form in create page.
  • form_edit_rules: List of form rules to manage rendering and behaviour of form in edit page.

class UserAdmin(ModelView):
    model = User
    form_columns = [User.name]
    form_args = dict(name=dict(label="Full name"))
    form_widget_args = dict(email=dict(readonly=True))
    form_overrides = dict(email=wtforms.EmailField)
    form_create_rules = ["name", "password"]
    form_edit_rules = ["name"]

Export and General options

There are few options which applied to both List or Edit page.
  • column_labels: A mapping of column labels, used to map column names to new names in all places.
  • save_as: A boolean to enable “save as new” option when editing an object.
  • save_as_continue: A boolean to control the redirect URL if save_as is enabled.
  • can_export: If the model can be exported. Default value is True.
  • column_export_list: List of columns to include in the export data. Default is all.
  • column_export_exclude_list: List of columns to exclude in the export data.
  • export_max_rows: Maximum number of rows to be exported. Default value is 0 which means unlimited.
  • export_types: List of export types to be enabled. Default value is [“csv”,“json”].

class UserAdmin(ModelView):
    model = User
    column_labels = {User.mail: "Email"}
    save_as = True
    column_export_list = [User.name, User.mail]
    export_max_rows = 20
    export_types = ['csv']

Built in Column Filters

Different filters are available which get implemented automatically based on model fields for_eg: column_filters = [User.name, "is_admin"]. All filters have a default value of “all” which allows the user to display all records.
  • BooleanFilter - A filter for boolean columns, with the values of Yes (true) and No (false)
  • AllUniqueStringValuesFilter - A filter for string columns, with the values of all unique values in the column
  • EnumFilter - A filter for enum columns which uses the values defined in the corresponding Enum class for filtering options
  • DateFieldFilter - A filter for Date and DateTime columns. It supports relative filter options such as Today, Past 7 Days and This Year and also supports __isnull suffix to filter rows where the date field is NULL
  • ForeignKeyFilter - A filter for relationship column which allows filtering based on values from the related model associated with the foreign key relationship
You can define filters for your model by specifying the desired fields in column_filters. The appropriate filter class is automatically applied based on the column type.

class BookAdmin(ModelView):
    model = Book
    column_filters = [
        'title',      # Uses AllUniqueStringValuesFilter
        'is_new',     # Uses BooleanFilter
        'author',     # Uses ForeignKeyFilter (based on relationship field)
        'status',     # Uses EnumFilter (based on Enum field)
        'created_at'  # Uses DateFieldFilter (for Date/DateTime columns)
        
        # Note: 'author_id' will raise an InvalidField error
        # since direct filtering on foreign key columns is not supported
    ]

admin.add_view(BookAdmin)