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
9cc1516c
Commit
9cc1516c
authored
Oct 25, 2012
by
Serge S. Koval
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed #101. None will be displayed as empty string by default.
parent
6878bd38
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
4 deletions
+82
-4
simple.py
examples/sqla/simple.py
+1
-0
base.py
flask_admin/model/base.py
+44
-4
typefmt.py
flask_admin/model/typefmt.py
+37
-0
No files found.
examples/sqla/simple.py
View file @
9cc1516c
...
...
@@ -53,6 +53,7 @@ class Post(db.Model):
class
Tag
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
name
=
db
.
Column
(
db
.
Unicode
(
64
))
value
=
db
.
Column
(
db
.
Boolean
())
def
__unicode__
(
self
):
return
self
.
name
...
...
flask_admin/model/base.py
View file @
9cc1516c
...
...
@@ -6,7 +6,7 @@ from flask.ext.admin.babel import gettext
from
flask.ext.admin.base
import
BaseView
,
expose
from
flask.ext.admin.tools
import
rec_getattr
from
flask.ext.admin.model
import
filters
from
flask.ext.admin.model
import
filters
,
typefmt
from
flask.ext.admin.actions
import
ActionsMixin
...
...
@@ -90,6 +90,35 @@ class BaseModelView(BaseView, ActionsMixin):
pass
"""
list_type_formatters
=
None
"""
Dictionary of value type formatters to be used in list view.
By default, two types are formatted:
1. ``None`` will be displayed as empty string
2. ``bool`` will be displayed as checkbox
If you don't like default behavior and don't want any type formatters
applied, just override this property with empty dictionary::
class MyModelView(BaseModelView):
list_type_formatters = dict()
If you don't want to display `NULL` instead of empty string, you can do
something like this::
from flask.ext.admin import typefmt
MY_DEFAULT_FORMATTERS = dict(typefmt.DEFAULT_FORMATTERS).extend({
type(None): typefmt.null_formatter
})
class MyModelView(BaseModelView):
list_type_formatters = MY_DEFAULT_FORMATTERS
Type formatters have lower priority than list column formatters.
"""
rename_columns
=
None
"""
Dictionary where key is column name and value is string to display.
...
...
@@ -281,6 +310,10 @@ class BaseModelView(BaseView, ActionsMixin):
# Filters
self
.
_filters
=
self
.
get_filters
()
# Type formatters
if
self
.
list_type_formatters
is
None
:
self
.
list_type_formatters
=
dict
(
typefmt
.
DEFAULT_FORMATTERS
)
if
self
.
_filters
:
self
.
_filter_groups
=
[]
self
.
_filter_dict
=
dict
()
...
...
@@ -707,10 +740,17 @@ class BaseModelView(BaseView, ActionsMixin):
:param name:
Field name
"""
if
name
in
self
.
list_formatters
:
return
self
.
list_formatters
[
name
](
context
,
model
,
name
)
column_fmt
=
self
.
list_formatters
.
get
(
name
)
if
column_fmt
is
not
None
:
return
column_fmt
(
context
,
model
,
name
)
value
=
rec_getattr
(
model
,
name
)
type_fmt
=
self
.
list_type_formatters
.
get
(
type
(
value
))
if
type_fmt
is
not
None
:
value
=
type_fmt
(
value
)
return
rec_getattr
(
model
,
name
)
return
value
# Views
@
expose
(
'/'
)
...
...
flask_admin/model/typefmt.py
0 → 100644
View file @
9cc1516c
from
jinja2
import
Markup
def
null_formatter
(
value
):
"""
Return `NULL` as the string for `None` value
:param value:
Value to check
"""
return
Markup
(
'<i>NULL</i>'
)
def
empty_formatter
(
value
):
"""
Return empty string for `None` value
:param value:
Value to check
"""
return
''
def
bool_formatter
(
value
):
"""
Return check icon if value is `True` or empty string otherwise.
:param value:
Value to check
"""
return
Markup
(
'<i class="icon-ok"></i>'
if
value
else
''
)
DEFAULT_FORMATTERS
=
{
type
(
None
):
empty_formatter
,
bool
:
bool_formatter
}
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