On Wed, Aug 29, 2018 at 2:17 PM,  <[email protected]> wrote:
> Hi !
>
> I'm using Python 3.6.5, Flask 1.0.2, SQLAlchemy 1.0.5 and I want to define
> an attribute as a maximum between other two, based on 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)

when you use a hybrid_property you need to ensure the expression you
are returning is also suitable as a SQL expression, so your code
should read:

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)


    @max.expression
    def max(cls):
        return func.max(cls.height, cls.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)
>
>
> But it fails, raising this error:
>
>     raise TypeError("Boolean value of this clause is not defined")
>
>
> Traceback:
>
> /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")
>
>
> I've tried with the max sqlalchemy function:
>
>
> return func.max(self.height, self.width)
>
>
> But it returns literally a function not a value.
>
>
> I also tried to use concept of this answer, without success.
>
>
> Any idea?
>
>
> Thanks!
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to