[英]How to do select with where on second order ForeignKeyField in Python Peewee ORM?
[英]Peewee Python ORM: assign query result to ForeignKeyField
我有2个模型,它们之间的一对多关系非常简单。 我正在尝试将查询其中一个的结果分配给另一个实体,但是我不知道该怎么做。 这是代码:
主类继承自2个类,并且__init__
方法已重载
class Bot(MySQLDatabase, ClientXMPP):
room = ForeignKeyField(Room)
在某个时候,我查询并尝试分配:
def __init__(self, ..., ..., room):
DBConnection.connect()
self.room = Room.get(...)
self.save()
但这引发了这个异常:
Traceback (most recent call last):
File "main.py", line 25, in <module>
xmpp = Bot(..., ..., room)
File "/home/.../bot.py", line 29, in __init__
self.room = room
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 724, in __set__
instance._data[self.att_name] = value.get_id()
TypeError: 'NoneType' object does not support item assignment
我刚刚开始使用该库,所以这可能是由于对文档的误解。
我知道这是一个老问题,但是如果其他人遇到此错误,我会找到此链接: Peewee模型在启动时执行初始化 ,因此您必须在任何peewee模型构造函数中调用super。
所以,
def __init__(self, ..., ..., room):
DBConnection.connect()
self.room = Room.get(...)
self.save()
会成为:
def __init__(self, ..., ..., room):
super(Bot, self).__init__()
DBConnection.connect()
self.room = Room.get(...)
self.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.