Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
flask-admin
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Python-Dev
flask-admin
Commits
9f378fae
Commit
9f378fae
authored
Jan 30, 2013
by
Christopher Toth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor English corrections for documentation of the base model class.
parent
fe3c035e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
40 deletions
+39
-40
base.py
flask_admin/model/base.py
+39
-40
No files found.
flask_admin/model/base.py
View file @
9f378fae
...
@@ -14,17 +14,17 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -14,17 +14,17 @@ class BaseModelView(BaseView, ActionsMixin):
"""
"""
Base model view.
Base model view.
View does not make any assumptions on how models are stored or managed, but expects
following:
This view does not make any assumptions on how models are stored or managed, but expects the
following:
1.
M
odel is an object
1.
The provided m
odel is an object
2.
M
odel contains properties
2.
The m
odel contains properties
3. Each model contains a
ttribute which uniquely identifies it (i.e. primary key for
database model)
3. Each model contains a
n attribute which uniquely identifies it (i.e. a primary key for a
database model)
4.
You can get
list of sorted models with pagination applied from a data source
4.
It is possible to retrieve a
list of sorted models with pagination applied from a data source
5. You can get one model by its identifier from the data source
5. You can get one model by its identifier from the data source
Essentially, if you want to support
new data store, all you have to do
:
Essentially, if you want to support
a new data store, all you have to do is
:
1. Derive from `BaseModelView` class
1. Derive from
the
`BaseModelView` class
2. Implement various data-related methods (`get_list`, `get_one`, `create_model`, etc)
2. Implement various data-related methods (`get_list`, `get_one`, `create_model`, etc)
3. Implement automatic form generation from the model representation (`scaffold_form`)
3. Implement automatic form generation from the model representation (`scaffold_form`)
"""
"""
...
@@ -81,7 +81,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -81,7 +81,7 @@ class BaseModelView(BaseView, ActionsMixin):
class MyModelView(BaseModelView):
class MyModelView(BaseModelView):
column_formatters = dict(price=lambda c, m, p: m.price*2)
column_formatters = dict(price=lambda c, m, p: m.price*2)
Callback function has following
prototype::
The Callback function has the
prototype::
def formatter(context, model, name):
def formatter(context, model, name):
# context is instance of jinja2.runtime.Context
# context is instance of jinja2.runtime.Context
...
@@ -92,19 +92,19 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -92,19 +92,19 @@ class BaseModelView(BaseView, ActionsMixin):
column_type_formatters
=
ObsoleteAttr
(
'column_type_formatters'
,
'list_type_formatters'
,
None
)
column_type_formatters
=
ObsoleteAttr
(
'column_type_formatters'
,
'list_type_formatters'
,
None
)
"""
"""
Dictionary of value type formatters to be used in list view.
Dictionary of value type formatters to be used in
the
list view.
By default, two types are formatted:
By default, two types are formatted:
1. ``None`` will be displayed as empty string
1. ``None`` will be displayed as
an
empty string
2. ``bool`` will be displayed as
chec
k if it is ``True``
2. ``bool`` will be displayed as
a checkmar
k if it is ``True``
If you don't like default behavior and don't want any type formatters
If you don't like
the
default behavior and don't want any type formatters
applied, just override this property with empty dictionary::
applied, just override this property with
an
empty dictionary::
class MyModelView(BaseModelView):
class MyModelView(BaseModelView):
column_type_formatters = dict()
column_type_formatters = dict()
If you want to display `NULL` instead of empty string, you can do
If you want to display `NULL` instead of
an
empty string, you can do
something like this::
something like this::
from flask.ext.admin import typefmt
from flask.ext.admin import typefmt
...
@@ -155,13 +155,13 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -155,13 +155,13 @@ class BaseModelView(BaseView, ActionsMixin):
column_sortable_list = ('name', 'last_name')
column_sortable_list = ('name', 'last_name')
If you want to explicitly specify field/column to be used while
If you want to explicitly specify field/column to be used while
sorting, you can use tuple::
sorting, you can use
a
tuple::
class MyModelView(BaseModelView):
class MyModelView(BaseModelView):
column_sortable_list = ('name', ('user', 'user.username'))
column_sortable_list = ('name', ('user', 'user.username'))
When using SQLAlchemy models, model attributes can be used instead
When using SQLAlchemy models, model attributes can be used instead
of
the string
::
of
strings
::
class MyModelView(BaseModelView):
class MyModelView(BaseModelView):
column_sortable_list = ('name', ('user', User.username))
column_sortable_list = ('name', ('user', User.username))
...
@@ -171,9 +171,9 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -171,9 +171,9 @@ class BaseModelView(BaseView, ActionsMixin):
'searchable_columns'
,
'searchable_columns'
,
None
)
None
)
"""
"""
C
ollection of the searchable columns. It is assumed that only
A c
ollection of the searchable columns. It is assumed that only
text-only fields are searchable, but it is up
for a
model
text-only fields are searchable, but it is up
to the
model
implementation to
make decision
.
implementation to
decide
.
Example::
Example::
...
@@ -197,7 +197,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -197,7 +197,7 @@ class BaseModelView(BaseView, ActionsMixin):
'list_display_pk'
,
'list_display_pk'
,
False
)
False
)
"""
"""
Controls if
primary key should be displayed in
list view.
Controls if
the primary key should be displayed in the
list view.
"""
"""
form
=
None
form
=
None
...
@@ -274,7 +274,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -274,7 +274,7 @@ class BaseModelView(BaseView, ActionsMixin):
# Various settings
# Various settings
page_size
=
20
page_size
=
20
"""
"""
Default page size.
Default page size
for pagination
.
"""
"""
def
__init__
(
self
,
model
,
def
__init__
(
self
,
model
,
...
@@ -285,11 +285,11 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -285,11 +285,11 @@ class BaseModelView(BaseView, ActionsMixin):
:param model:
:param model:
Model class
Model class
:param name:
:param name:
View name. If not provided, will use model class name
View name. If not provided, will use
the
model class name
:param category:
:param category:
View category
View category
:param endpoint:
:param endpoint:
Base endpoint. If not provided, will use model name + 'view'.
Base endpoint. If not provided, will use
the
model name + 'view'.
For example if model name was 'User', endpoint will be
For example if model name was 'User', endpoint will be
'userview'
'userview'
:param url:
:param url:
...
@@ -383,7 +383,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -383,7 +383,7 @@ class BaseModelView(BaseView, ActionsMixin):
def
get_column_name
(
self
,
field
):
def
get_column_name
(
self
,
field
):
"""
"""
Return human-readable column name.
Return
a
human-readable column name.
:param field:
:param field:
Model field name.
Model field name.
...
@@ -395,9 +395,9 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -395,9 +395,9 @@ class BaseModelView(BaseView, ActionsMixin):
def
get_list_columns
(
self
):
def
get_list_columns
(
self
):
"""
"""
Returns list of the model field names. If `column_list` was
Returns
a
list of the model field names. If `column_list` was
set, returns it. Otherwise calls `scaffold_list_columns`
set, returns it. Otherwise calls `scaffold_list_columns`
to generate list from the model.
to generate
the
list from the model.
"""
"""
columns
=
self
.
column_list
columns
=
self
.
column_list
...
@@ -415,14 +415,14 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -415,14 +415,14 @@ class BaseModelView(BaseView, ActionsMixin):
Returns dictionary of sortable columns. Must be implemented in
Returns dictionary of sortable columns. Must be implemented in
the child class.
the child class.
Expected return format is
dictionary, where key is field name
and
Expected return format is
a dictionary, where keys are field names
and
value
is property name
.
value
s are property names
.
"""
"""
raise
NotImplemented
(
'Please implement scaffold_sortable_columns method'
)
raise
NotImplemented
(
'Please implement scaffold_sortable_columns method'
)
def
get_sortable_columns
(
self
):
def
get_sortable_columns
(
self
):
"""
"""
Returns dictionary of the sortable columns. Key is a model
Returns
a
dictionary of the sortable columns. Key is a model
field name and value is sort column (for example - attribute).
field name and value is sort column (for example - attribute).
If `column_sortable_list` is set, will use it. Otherwise, will call
If `column_sortable_list` is set, will use it. Otherwise, will call
...
@@ -459,10 +459,10 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -459,10 +459,10 @@ class BaseModelView(BaseView, ActionsMixin):
def
is_valid_filter
(
self
,
filter
):
def
is_valid_filter
(
self
,
filter
):
"""
"""
Verify that provided filter object is valid.
Verify that
the
provided filter object is valid.
Override in model backend implementation to verify if
Override in model backend implementation to verify if
provided filter type is allowed.
the
provided filter type is allowed.
:param filter:
:param filter:
Filter object to verify.
Filter object to verify.
...
@@ -471,7 +471,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -471,7 +471,7 @@ class BaseModelView(BaseView, ActionsMixin):
def
get_filters
(
self
):
def
get_filters
(
self
):
"""
"""
Return list of filter objects.
Return
a
list of filter objects.
If your model backend implementation does not support filters,
If your model backend implementation does not support filters,
override this method and return `None`.
override this method and return `None`.
...
@@ -568,10 +568,9 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -568,10 +568,9 @@ class BaseModelView(BaseView, ActionsMixin):
# Database-related API
# Database-related API
def
get_list
(
self
,
page
,
sort_field
,
sort_desc
,
search
,
filters
):
def
get_list
(
self
,
page
,
sort_field
,
sort_desc
,
search
,
filters
):
"""
"""
Return list of models from the data source with applied pagination
Return a paginated and sorted list of models from the data source.
and sorting.
Must be implemented in the child class.
Must be implemented in child class.
:param page:
:param page:
Page number, 0 based. Can be set to None if it is first page.
Page number, 0 based. Can be set to None if it is first page.
...
@@ -601,7 +600,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -601,7 +600,7 @@ class BaseModelView(BaseView, ActionsMixin):
# Model handlers
# Model handlers
def
on_model_change
(
self
,
form
,
model
):
def
on_model_change
(
self
,
form
,
model
):
"""
"""
Allow to do some actions after a model wa
s created or updated.
Perform some actions after a model i
s created or updated.
Called from create_model and update_model in the same transaction
Called from create_model and update_model in the same transaction
(if it has any meaning for a store backend).
(if it has any meaning for a store backend).
...
@@ -612,7 +611,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -612,7 +611,7 @@ class BaseModelView(BaseView, ActionsMixin):
def
on_model_delete
(
self
,
model
):
def
on_model_delete
(
self
,
model
):
"""
"""
Allow to do some actions before a model will be
deleted.
Perform some actions before a model is
deleted.
Called from delete_model in the same transaction
Called from delete_model in the same transaction
(if it has any meaning for a store backend).
(if it has any meaning for a store backend).
...
@@ -753,7 +752,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -753,7 +752,7 @@ class BaseModelView(BaseView, ActionsMixin):
Override this method to allow or disallow actions based
Override this method to allow or disallow actions based
on some condition.
on some condition.
Default implementation only checks if
particular action
The default implementation only checks if the
particular action
is not in `action_disallowed_list`.
is not in `action_disallowed_list`.
"""
"""
return
name
not
in
self
.
action_disallowed_list
return
name
not
in
self
.
action_disallowed_list
...
@@ -761,7 +760,7 @@ class BaseModelView(BaseView, ActionsMixin):
...
@@ -761,7 +760,7 @@ class BaseModelView(BaseView, ActionsMixin):
@
contextfunction
@
contextfunction
def
get_list_value
(
self
,
context
,
model
,
name
):
def
get_list_value
(
self
,
context
,
model
,
name
):
"""
"""
Returns
value to be displayed in
list view
Returns
the value to be displayed in the
list view
:param context:
:param context:
:py:class:`jinja2.runtime.Context`
:py:class:`jinja2.runtime.Context`
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment