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
406ab211
Commit
406ab211
authored
Aug 19, 2012
by
Serge S. Koval
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for list formatting callbacks in model admin. Fixed #43 and #23
parent
54b505ca
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
1 deletion
+34
-1
simple.py
examples/sqla/simple.py
+1
-0
base.py
flask_admin/model/base.py
+33
-1
No files found.
examples/sqla/simple.py
View file @
406ab211
...
...
@@ -94,6 +94,7 @@ class PostAdmin(sqlamodel.ModelView):
# Just call parent class with predefined model.
super
(
PostAdmin
,
self
)
.
__init__
(
Post
,
session
)
if
__name__
==
'__main__'
:
# Create admin
admin
=
admin
.
Admin
(
app
,
'Simple Models'
)
...
...
flask_admin/model/base.py
View file @
406ab211
...
...
@@ -69,6 +69,24 @@ class BaseModelView(BaseView, ActionsMixin):
excluded_list_columns = ('last_name', 'email')
"""
list_formatters
=
dict
()
"""
Dictionary of list view column formatters.
For example, if you want to show price multiplied by
two, you can do something like this::
class MyModelView(BaseModelView):
list_formatters = dict(price=lambda m, p: m.price*2)
Callback function has following prototype::
def formatter(model, name):
# model is model instance
# name is property name
pass
"""
rename_columns
=
None
"""
Dictionary where key is column name and value is string to display.
...
...
@@ -647,6 +665,20 @@ class BaseModelView(BaseView, ActionsMixin):
"""
return
name
not
in
self
.
disallowed_actions
def
get_list_value
(
self
,
model
,
name
):
"""
Returns value to be displayed in list view
`model`
Model instance
`name`
Field name
"""
if
name
in
self
.
list_formatters
:
return
self
.
list_formatters
[
name
](
model
,
name
)
return
rec_getattr
(
model
,
name
)
# Views
@
expose
(
'/'
)
def
index_view
(
self
):
...
...
@@ -711,7 +743,7 @@ class BaseModelView(BaseView, ActionsMixin):
# Stuff
enumerate
=
enumerate
,
get_pk_value
=
self
.
get_pk_value
,
get_value
=
rec_getattr
,
get_value
=
self
.
get_list_value
,
return_url
=
self
.
_get_url
(
'.index_view'
,
page
,
sort_idx
,
...
...
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