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
7f97396d
Commit
7f97396d
authored
Aug 31, 2016
by
Serge S. Koval
Committed by
GitHub
Aug 31, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1323 from vToMy/feature/hybrid_property_detection
Updated hybrid property detection to new sqlalchemy API
parents
0cd24273
4d79c706
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
9 deletions
+21
-9
app.py
examples/sqla-hybrid_property/app.py
+4
-6
tools.py
flask_admin/contrib/sqla/tools.py
+15
-1
view.py
flask_admin/contrib/sqla/view.py
+2
-2
No files found.
examples/sqla-hybrid_property/app.py
View file @
7f97396d
...
...
@@ -4,7 +4,6 @@ from sqlalchemy.ext.hybrid import hybrid_property
import
flask_admin
as
admin
from
flask_admin.contrib
import
sqla
from
flask_admin.contrib.sqla.filters
import
IntGreaterFilter
# Create application
app
=
Flask
(
__name__
)
...
...
@@ -36,14 +35,13 @@ class Screen(db.Model):
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.
'''
"""
Flask-admin can not automatically find a hybrid_property yet. You will
need to manually define the column in list_view/filters/sorting/etc.
"""
list_columns
=
[
'id'
,
'width'
,
'height'
,
'number_of_pixels'
]
column_sortable_list
=
[
'id'
,
'width'
,
'height'
,
'number_of_pixels'
]
# make sure the type of your filter matches your hybrid_property
column_filters
=
[
IntGreaterFilter
(
Screen
.
number_of_pixels
,
'Number of Pixels'
)]
# Flask-admin can automatically detect the relevant filters for hybrid properties.
column_filters
=
(
'number_of_pixels'
,
)
# Create admin
...
...
flask_admin/contrib/sqla/tools.py
View file @
7f97396d
from
sqlalchemy
import
tuple_
,
or_
,
and_
from
sqlalchemy
import
tuple_
,
or_
,
and_
,
inspect
from
sqlalchemy.ext.hybrid
import
hybrid_property
from
sqlalchemy.sql.operators
import
eq
from
sqlalchemy.exc
import
DBAPIError
from
sqlalchemy.orm.attributes
import
InstrumentedAttribute
...
...
@@ -170,3 +171,16 @@ def get_field_with_path(model, name):
path
.
append
(
column
.
table
)
return
attr
,
path
# copied from sqlalchemy-utils
def
get_hybrid_properties
(
model
):
return
dict
(
(
key
,
prop
)
for
key
,
prop
in
inspect
(
model
)
.
all_orm_descriptors
.
items
()
if
isinstance
(
prop
,
hybrid_property
)
)
def
is_hybrid_property
(
model
,
attr_name
):
return
attr_name
in
get_hybrid_properties
(
model
)
flask_admin/contrib/sqla/view.py
View file @
7f97396d
...
...
@@ -4,7 +4,7 @@ import inspect
from
sqlalchemy.orm.attributes
import
InstrumentedAttribute
from
sqlalchemy.orm
import
joinedload
,
aliased
from
sqlalchemy.sql.expression
import
desc
,
ColumnElement
from
sqlalchemy.sql.expression
import
desc
from
sqlalchemy
import
Boolean
,
Table
,
func
,
or_
from
sqlalchemy.exc
import
IntegrityError
from
sqlalchemy.sql.expression
import
cast
...
...
@@ -603,7 +603,7 @@ class ModelView(BaseModelView):
return
filters
else
:
is_hybrid_property
=
isinstance
(
attr
,
ColumnElement
)
is_hybrid_property
=
tools
.
is_hybrid_property
(
self
.
model
,
name
)
if
is_hybrid_property
:
column
=
attr
else
:
...
...
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