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
2d6f7dd6
Commit
2d6f7dd6
authored
Nov 14, 2018
by
Sergey Shubin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Alternative field for many-to-many relationship, appears as list of
checkboxes
parent
3bd34b10
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
0 deletions
+54
-0
fields.py
flask_admin/contrib/sqla/fields.py
+25
-0
widgets.py
flask_admin/contrib/sqla/widgets.py
+29
-0
No files found.
flask_admin/contrib/sqla/fields.py
View file @
2d6f7dd6
...
@@ -13,6 +13,7 @@ except ImportError:
...
@@ -13,6 +13,7 @@ except ImportError:
from
.tools
import
get_primary_key
from
.tools
import
get_primary_key
from
flask_admin._compat
import
text_type
,
string_types
,
iteritems
from
flask_admin._compat
import
text_type
,
string_types
,
iteritems
from
flask_admin.contrib.sqla.widgets
import
CheckboxListInput
from
flask_admin.form
import
FormOpts
,
BaseForm
,
Select2Widget
from
flask_admin.form
import
FormOpts
,
BaseForm
,
Select2Widget
from
flask_admin.model.fields
import
InlineFieldList
,
InlineModelFormField
from
flask_admin.model.fields
import
InlineFieldList
,
InlineModelFormField
from
flask_admin.babel
import
lazy_gettext
from
flask_admin.babel
import
lazy_gettext
...
@@ -181,6 +182,30 @@ class QuerySelectMultipleField(QuerySelectField):
...
@@ -181,6 +182,30 @@ class QuerySelectMultipleField(QuerySelectField):
raise
ValidationError
(
self
.
gettext
(
u'Not a valid choice'
))
raise
ValidationError
(
self
.
gettext
(
u'Not a valid choice'
))
class
CheckboxListField
(
QuerySelectMultipleField
):
"""
Alternative field for many-to-many relationships.
Can be used instead of `QuerySelectMultipleField`.
Appears as the list of checkboxes.
Example::
class MyView(ModelView):
form_columns = (
'languages',
)
form_args = {
'languages': {
'query_factory': Language.query,
},
}
form_overrides = {
'languages': CheckboxListField,
}
"""
widget
=
CheckboxListInput
()
class
HstoreForm
(
BaseForm
):
class
HstoreForm
(
BaseForm
):
""" Form used in InlineFormField/InlineHstoreList for HSTORE columns """
""" Form used in InlineFormField/InlineHstoreList for HSTORE columns """
key
=
StringField
(
lazy_gettext
(
'Key'
))
key
=
StringField
(
lazy_gettext
(
'Key'
))
...
...
flask_admin/contrib/sqla/widgets.py
0 → 100644
View file @
2d6f7dd6
from
wtforms.widgets.core
import
HTMLString
,
escape_html
class
CheckboxListInput
:
"""
Alternative widget for many-to-many relationships.
Appears as the list of checkboxes.
"""
template
=
(
'<div class="checkbox">'
' <label>'
' <input id="
%(id)
s" name="
%(name)
s" value="
%(id)
s" '
'type="checkbox"
%(selected)
s>
%(label)
s'
' </label>'
'</div>'
)
def
__call__
(
self
,
field
,
**
kwargs
):
items
=
[]
for
val
,
label
,
selected
in
field
.
iter_choices
():
args
=
{
'id'
:
val
,
'name'
:
field
.
name
,
'label'
:
escape_html
(
label
,
quote
=
False
),
'selected'
:
' checked'
if
selected
else
''
,
}
items
.
append
(
self
.
template
%
args
)
return
HTMLString
(
''
.
join
(
items
))
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