尝试在column_property中使用float()
我试图在使用 column_property 分配列时将列转换为浮点数:
class voteinfo(Base):
__tablename__ = 'voteinfo'
id = Column(Integer, primary_key=True)
upvotes = Column(Integer)
downvotes = Column(Integer)
controversial = column_property(float(upvotes - downvotes)/(abs(upvotes + downvotes)+1)
def __init__(self, upvotes, downvotes):
self.upvotes = upvotes
self.downvotes = downvotes
但是,当我运行此命令时,出现以下错误:
TypeError: float() argument must be a string or a number
有更好的方法来执行此操作吗?我使用 column_property 是因为我希望能够按有争议的进行排序。
I'm trying to convert a column into a float while assigning a column using column_property:
class voteinfo(Base):
__tablename__ = 'voteinfo'
id = Column(Integer, primary_key=True)
upvotes = Column(Integer)
downvotes = Column(Integer)
controversial = column_property(float(upvotes - downvotes)/(abs(upvotes + downvotes)+1)
def __init__(self, upvotes, downvotes):
self.upvotes = upvotes
self.downvotes = downvotes
However, when I run this, I get the following error:
TypeError: float() argument must be a string or a number
Is there a better way to do this? I'm using column_property because I want to be able to sort by controversial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 sqlalchemy 文档 中,最好的方法是这样做的目的是定义一个 python
@property
。您还需要使用内置的 sqlalchemy float 类型。From the sqlalchemy docs, the best way to do this is to define a python
@property
. You also need to use the built-in sqlalchemy float type.