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
dddc2a8f
Commit
dddc2a8f
authored
Oct 18, 2018
by
PJ Janse van Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge 'hybrid-property' example into 'sqla' example.
parent
77ea94ff
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
91 deletions
+25
-91
README.rst
examples/sqla-hybrid_property/README.rst
+0
-30
__init__.py
examples/sqla-hybrid_property/__init__.py
+0
-1
app.py
examples/sqla-hybrid_property/app.py
+0
-57
requirements.txt
examples/sqla-hybrid_property/requirements.txt
+0
-3
app.py
examples/sqla/app.py
+25
-0
No files found.
examples/sqla-hybrid_property/README.rst
deleted
100644 → 0
View file @
77ea94ff
Example of how to use (and filter on) a hybrid_property with the SQLAlchemy backend.
Hybrid properties allow you to treat calculations (for example: first_name + last_name)
like any other database column.
To run this example:
1. Clone the repository::
git clone https://github.com/flask-admin/flask-admin.git
cd flask-admin
2. Create and activate a virtual environment::
virtualenv env
source env/bin/activate
3. Install requirements::
pip install -r 'examples/sqla-hybrid_property/requirements.txt'
4. Run the application::
python examples/sqla-hybrid_property/app.py
The first time you run this example, a sample sqlite database gets populated automatically. To suppress this behaviour,
comment the following lines in app.py:::
if not os.path.exists(database_path):
build_sample_db()
examples/sqla-hybrid_property/__init__.py
deleted
100644 → 0
View file @
77ea94ff
examples/sqla-hybrid_property/app.py
deleted
100644 → 0
View file @
77ea94ff
from
flask
import
Flask
from
flask_sqlalchemy
import
SQLAlchemy
from
sqlalchemy.ext.hybrid
import
hybrid_property
import
flask_admin
as
admin
from
flask_admin.contrib
import
sqla
# Create application
app
=
Flask
(
__name__
)
# Create dummy secrey key so we can use sessions
app
.
config
[
'SECRET_KEY'
]
=
'123456790'
# Create in-memory database
app
.
config
[
'SQLALCHEMY_DATABASE_URI'
]
=
'sqlite:///sample_db_2.sqlite'
app
.
config
[
'SQLALCHEMY_ECHO'
]
=
True
db
=
SQLAlchemy
(
app
)
# Flask views
@
app
.
route
(
'/'
)
def
index
():
return
'<a href="/admin/">Click me to get to Admin!</a>'
class
Screen
(
db
.
Model
):
__tablename__
=
'screen'
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
width
=
db
.
Column
(
db
.
Integer
,
nullable
=
False
)
height
=
db
.
Column
(
db
.
Integer
,
nullable
=
False
)
@
hybrid_property
def
number_of_pixels
(
self
):
return
self
.
width
*
self
.
height
class
ScreenAdmin
(
sqla
.
ModelView
):
""" Flask-admin can not automatically find a hybrid_property yet. You will
need to manually define the column in list_view/filters/sorting/etc."""
column_list
=
[
'id'
,
'width'
,
'height'
,
'number_of_pixels'
]
column_sortable_list
=
[
'id'
,
'width'
,
'height'
,
'number_of_pixels'
]
# Flask-admin can automatically detect the relevant filters for hybrid properties.
column_filters
=
(
'number_of_pixels'
,
)
# Create admin
admin
=
admin
.
Admin
(
app
,
name
=
'Example: SQLAlchemy2'
,
template_mode
=
'bootstrap3'
)
admin
.
add_view
(
ScreenAdmin
(
Screen
,
db
.
session
))
if
__name__
==
'__main__'
:
# Create DB
db
.
create_all
()
# Start app
app
.
run
(
debug
=
True
)
examples/sqla-hybrid_property/requirements.txt
deleted
100644 → 0
View file @
77ea94ff
Flask
Flask-Admin
Flask-SQLAlchemy
examples/sqla/app.py
View file @
dddc2a8f
...
...
@@ -2,6 +2,7 @@ import os
import
os.path
as
op
from
flask
import
Flask
from
flask_sqlalchemy
import
SQLAlchemy
from
sqlalchemy.ext.hybrid
import
hybrid_property
from
wtforms
import
validators
...
...
@@ -94,6 +95,17 @@ class Tree(db.Model):
return
"{}"
.
format
(
self
.
name
)
class
Screen
(
db
.
Model
):
__tablename__
=
'screen'
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
width
=
db
.
Column
(
db
.
Integer
,
nullable
=
False
)
height
=
db
.
Column
(
db
.
Integer
,
nullable
=
False
)
@
hybrid_property
def
number_of_pixels
(
self
):
return
self
.
width
*
self
.
height
# Flask views
@
app
.
route
(
'/'
)
def
index
():
...
...
@@ -171,6 +183,14 @@ class TreeView(sqla.ModelView):
form_excluded_columns
=
[
'children'
,
]
class
ScreenView
(
sqla
.
ModelView
):
column_list
=
[
'id'
,
'width'
,
'height'
,
'number_of_pixels'
]
# not that 'number_of_pixels' is a hybrid property, not a field
column_sortable_list
=
[
'id'
,
'width'
,
'height'
,
'number_of_pixels'
]
# Flask-admin can automatically detect the relevant filters for hybrid properties.
column_filters
=
(
'number_of_pixels'
,
)
# Create admin
admin
=
admin
.
Admin
(
app
,
name
=
'Example: SQLAlchemy'
,
template_mode
=
'bootstrap3'
)
...
...
@@ -180,6 +200,7 @@ admin.add_view(sqla.ModelView(Tag, db.session))
admin
.
add_view
(
PostAdmin
(
db
.
session
))
admin
.
add_view
(
sqla
.
ModelView
(
UserInfo
,
db
.
session
,
category
=
"Other"
))
admin
.
add_view
(
TreeView
(
Tree
,
db
.
session
,
category
=
"Other"
))
admin
.
add_view
(
ScreenView
(
Screen
,
db
.
session
,
category
=
"Other"
))
admin
.
add_sub_category
(
name
=
"Links"
,
parent_name
=
"Other"
)
admin
.
add_link
(
MenuLink
(
name
=
'Back Home'
,
url
=
'/'
,
category
=
'Links'
))
admin
.
add_link
(
MenuLink
(
name
=
'Google'
,
url
=
'http://www.google.com/'
,
category
=
'Links'
))
...
...
@@ -215,6 +236,7 @@ def build_sample_db():
user
.
first_name
=
first_names
[
i
]
user
.
last_name
=
last_names
[
i
]
user
.
email
=
first_names
[
i
]
.
lower
()
+
"@example.com"
user
.
info
.
append
(
UserInfo
(
key
=
"foo"
,
value
=
"bar"
))
user_list
.
append
(
user
)
db
.
session
.
add
(
user
)
...
...
@@ -290,6 +312,9 @@ def build_sample_db():
leaf
.
parent
=
branch
db
.
session
.
add
(
leaf
)
db
.
session
.
add
(
Screen
(
width
=
500
,
height
=
2000
))
db
.
session
.
add
(
Screen
(
width
=
550
,
height
=
1900
))
db
.
session
.
commit
()
return
...
...
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