我正在使用Python 3.6.5,Flask 1.0.2,SQLAlchemy 1.0.5,并且我想基于flask-admin hybrid property example将其他两个属性之间的最大值定义为:
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
from sqlalchemy.sql.expression import func
# 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 max(self):
return max(self.height, self.width)
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', 'max']
column_sortable_list = ['id', 'width', 'height', 'max']
# Flask-admin can automatically detect the relevant filters for hybrid properties.
column_filters = ('max', )
# 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)
但是它失败了,引发了这个错误:
raise TypeError("Boolean value of this clause is not defined")
跟踪:
/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
File "app.py", line 49, in <module>
admin.add_view(ScreenAdmin(Screen, db.session))
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/flask_admin/contrib/sqla/view.py", line 329, in __init__
menu_icon_value=menu_icon_value)
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/flask_admin/model/base.py", line 804, in __init__
self._refresh_cache()
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/flask_admin/model/base.py", line 881, in _refresh_cache
self._list_columns = self.get_list_columns()
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/flask_admin/model/base.py", line 1022, in get_list_columns
excluded_columns=self.column_exclude_list,
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/flask_admin/contrib/sqla/view.py", line 531, in get_column_names
column, path = tools.get_field_with_path(self.model, c)
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/flask_admin/contrib/sqla/tools.py", line 150, in get_field_with_path
value = getattr(current_model, attribute)
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/sqlalchemy/ext/hybrid.py", line 867, in __get__
return self._expr_comparator(owner)
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/sqlalchemy/ext/hybrid.py", line 1066, in expr_comparator
owner, self.__name__, self, comparator(owner),
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/sqlalchemy/ext/hybrid.py", line 1055, in _expr
return ExprComparator(cls, expr(cls), self)
File "app.py", line 34, in number_of_pixels
return max(self.width,self.height)
File "/home/bibo/usr/miniconda/envs/otroflask/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 2975, in __bool__
raise TypeError("Boolean value of this clause is not defined")
我尝试使用最大sqlalchemy函数:
return func.max(self.height, self.width)
但是它实际上返回一个函数而不是一个值。
我也尝试使用this answer的概念,但没有成功。
答案 0 :(得分:0)
您可以尝试类似的操作:
-