我想知道有没有人知道如何在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'
我想知道我做错了什么?我的手指都被上网查得血淋淋的……队长!请你帮忙。
解决方案:
试试这个
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)