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
e04358d3
Commit
e04358d3
authored
Aug 25, 2015
by
Paul Brown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix custom filter example after breaking change
parent
bb519b47
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
5 deletions
+19
-5
app.py
examples/sqla-custom-filter/app.py
+19
-5
No files found.
examples/sqla-custom-filter/app.py
View file @
e04358d3
...
@@ -2,7 +2,7 @@ from flask import Flask
...
@@ -2,7 +2,7 @@ from flask import Flask
from
flask_sqlalchemy
import
SQLAlchemy
from
flask_sqlalchemy
import
SQLAlchemy
from
flask_admin.contrib
import
sqla
from
flask_admin.contrib
import
sqla
from
flask_admin
import
expose
,
Admin
from
flask_admin
import
Admin
# required for creating custom filters
# required for creating custom filters
from
flask_admin.contrib.sqla.filters
import
BaseSQLAFilter
,
FilterEqual
from
flask_admin.contrib.sqla.filters
import
BaseSQLAFilter
,
FilterEqual
...
@@ -19,6 +19,13 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE
...
@@ -19,6 +19,13 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE
app
.
config
[
'SQLALCHEMY_ECHO'
]
=
True
app
.
config
[
'SQLALCHEMY_ECHO'
]
=
True
db
=
SQLAlchemy
(
app
)
db
=
SQLAlchemy
(
app
)
# Flask views
@
app
.
route
(
'/'
)
def
index
():
return
'<a href="/admin/">Click me to get to Admin!</a>'
# Create model
# Create model
class
User
(
db
.
Model
):
class
User
(
db
.
Model
):
def
__init__
(
self
,
first_name
,
last_name
,
username
,
email
):
def
__init__
(
self
,
first_name
,
last_name
,
username
,
email
):
...
@@ -33,13 +40,14 @@ class User(db.Model):
...
@@ -33,13 +40,14 @@ class User(db.Model):
username
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
)
username
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
)
email
=
db
.
Column
(
db
.
String
(
120
),
unique
=
True
)
email
=
db
.
Column
(
db
.
String
(
120
),
unique
=
True
)
# Required for admin
istrative
interface. For python 3 please use __str__ instead.
# Required for admin interface. For python 3 please use __str__ instead.
def
__unicode__
(
self
):
def
__unicode__
(
self
):
return
self
.
username
return
self
.
username
# Create custom filter class
# Create custom filter class
class
FilterLastNameBrown
(
BaseSQLAFilter
):
class
FilterLastNameBrown
(
BaseSQLAFilter
):
def
apply
(
self
,
query
,
value
):
def
apply
(
self
,
query
,
value
,
alias
=
None
):
if
value
==
'1'
:
if
value
==
'1'
:
return
query
.
filter
(
self
.
column
==
"Brown"
)
return
query
.
filter
(
self
.
column
==
"Brown"
)
else
:
else
:
...
@@ -48,18 +56,23 @@ class FilterLastNameBrown(BaseSQLAFilter):
...
@@ -48,18 +56,23 @@ class FilterLastNameBrown(BaseSQLAFilter):
def
operation
(
self
):
def
operation
(
self
):
return
'is Brown'
return
'is Brown'
# Add custom filter and standard FilterEqual to ModelView
# Add custom filter and standard FilterEqual to ModelView
class
UserAdmin
(
sqla
.
ModelView
):
class
UserAdmin
(
sqla
.
ModelView
):
# each filter in the list is a filter operation (equals, not equals, etc)
# each filter in the list is a filter operation (equals, not equals, etc)
# filters with the same name will appear as operations under the same filter
# filters with the same name will appear as operations under the same filter
column_filters
=
[
column_filters
=
[
FilterEqual
(
User
.
last_name
,
'Last Name'
),
FilterEqual
(
User
.
last_name
,
'Last Name'
),
FilterLastNameBrown
(
User
.
last_name
,
'Last Name'
,
options
=
((
'1'
,
'Yes'
),(
'0'
,
'No'
)))
FilterLastNameBrown
(
User
.
last_name
,
'Last Name'
,
options
=
((
'1'
,
'Yes'
),
(
'0'
,
'No'
))
)
]
]
admin
=
Admin
(
app
,
template_mode
=
"bootstrap3"
)
admin
=
Admin
(
app
,
template_mode
=
"bootstrap3"
)
admin
.
add_view
(
UserAdmin
(
User
,
db
.
session
))
admin
.
add_view
(
UserAdmin
(
User
,
db
.
session
))
def
build_sample_db
():
def
build_sample_db
():
db
.
drop_all
()
db
.
drop_all
()
db
.
create_all
()
db
.
create_all
()
...
@@ -70,6 +83,7 @@ def build_sample_db():
...
@@ -70,6 +83,7 @@ def build_sample_db():
db
.
session
.
add_all
([
user_obj1
,
user_obj2
,
user_obj3
])
db
.
session
.
add_all
([
user_obj1
,
user_obj2
,
user_obj3
])
db
.
session
.
commit
()
db
.
session
.
commit
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
build_sample_db
()
build_sample_db
()
app
.
run
(
port
=
5000
)
app
.
run
(
port
=
5000
,
debug
=
True
)
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