简体   繁体   English

hybrid_property 装饰器:无法设置属性错误

[英]The hybrid_property decorator: can't set attribute error

I create an app in __init__.py as follows:我在__init__.py中创建了一个应用程序,如下所示:

from flask_bcrypt import Bcrypt

app = Flask(__name__, static_folder='static')
bcrypt = Bcrypt(app)

and I use this app instance when creating database models as follows:我在创建数据库模型时使用这个app实例,如下所示:

from sqlalchemy.ext.hybrid import hybrid_property
from app import bcrypt, db

class User(db.Model):

    name = db.Column(db.String, nullable=False)
    _password = db.Column(db.String)

    @hybrid_property
    def password(self) -> str:
        return self._password

    @password.setter
    def _set_password(self, password: str):
        self._password = bcrypt.generate_password_hash(password)

And I try to initiate an instance as follows (this is the way that I want to do it as it looks cleaner):我尝试按如下方式启动一个实例(这是我想做的方式,因为它看起来更干净):

user = User(name='foo', password='bar')

but when I commit this to a database I get the following error: AttributeError: can't set attribute .但是当我将其提交到数据库时,我收到以下错误: AttributeError: can't set attribute This clears if I use:如果我使用,这将清除:

user = User(name='foo', _password='bar')

I guess I'm not using the @hybrid_property decorator properly?我想我没有正确使用@hybrid_property装饰器? I want to use the user = User(name='foo', password='bar') syntax if possible.如果可能,我想使用user = User(name='foo', password='bar')语法。

Thanks for any help here.感谢您在这里的任何帮助。

Whether you use @hybrid_property decorator or a native python object's @property decorator you must retain same attribute name used in property definition for setter or getter methods.无论您使用@hybrid_property装饰器还是本机 python 对象的@property装饰器,您都必须为 setter 或 getter 方法保留在属性定义中使用的相同属性名称。

Here in your case def _set_password() should be def password() .在您的情况下, def _set_password()应该是def password() Your code should look like this:您的代码应如下所示:

 @hybrid_property
 def password(self):
     return self._password_hash

 @password.setter
 def password(self, plaintext):
     self._password_hash = generate_password_hash(plaintext)  

Detail Explanation: https://github.com/sqlalchemy/sqlalchemy/issues/4332详细说明: https://github.com/sqlalchemy/sqlalchemy/issues/4332

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置时出现SQLAlchemy hybrid_property错误 - SQLAlchemy hybrid_property error when setting Sqlalchemy hybrid_property / 表达式错误中的情况 - Case in Sqlalchemy hybrid_property / expression error 如何将SQLAlchemy的@hybrid_property装饰器与Werkzeug的cached_property装饰器结合在一起? - How to combine SQLAlchemy's @hybrid_property decorator with Werkzeug's cached_property decorator? sqlalchemy hybrid_property 无法在初始化时设置,关键字参数无效? - The sqlalchemy hybrid_property can not be set during initialization, invalid keyword argument? SQLAlchemy如何设置hybrid_property的键属性 - SQLAlchemy how to sets key attribute of hybrid_property sqlalchemy hybrid_property 无法在初始化时设置,关键字参数无效? - The sqlalchemy hybrid_property can not be set during initialization, invalid keyword argument? 使用 SQLAlchemy 的 hybrid_property 时,Flask 找不到“密码”字段 - Flask can't find 'password' field when using SQLAlchemy's hybrid_property mypy 无法识别具有 hybrid_property 的 SQLAlchemy 列 - mypy doesn't recognize SQLAlchemy columns with hybrid_property Flask-Admin:可以搜索 hybrid_property 吗? - Flask-Admin: can a hybrid_property be searchable? if语句在hybrid_property表达式中 - if statement in a hybrid_property expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM