Commit 7f97396d authored by Serge S. Koval's avatar Serge S. Koval Committed by GitHub

Merge pull request #1323 from vToMy/feature/hybrid_property_detection

Updated hybrid property detection to new sqlalchemy API
parents 0cd24273 4d79c706
......@@ -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
......
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)
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment