【问题标题】:SQL Alchemy - hybrid properties - splitting a columnSQLAlchemy - 混合属性 - 拆分列
【发布时间】:2020-04-23 16:12:22
【问题描述】:

我想知道是否有人知道如何在 sqlalchemy 中拆分列以创建计算字段。

试过了:

class Pupils(db.Model):
    full_name = db.Column(db.Text) #Jonh Doe

@hybrid_property
def firstname(self):
    return self.full_name.split(" ")[0]

但是,我收到错误消息:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Pupils.full_name has an attribute 'split'

我想知道我做错了什么?我的手指因为谷歌搜索而流血……船长!请帮忙!

【问题讨论】:

标签: python flask-sqlalchemy


【解决方案1】:

试试这个

from sqlalchemy import func

class Pupils(db.Model):
    full_name = db.Column(db.Text) #Jonh Doe

@hybrid_property
def firstname(self):
    return func.split_part(self.full_name, ' ', 1)

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多