我正在使用Python Peewee ORM,我想知道如何仅通过插入键而不是对象本身来插入外部字段。例如,我有一个名为message的模型,具有message_type,这是一个ForeignKeyField。我知道message_type的键是1,所以我尝试这样做:

>>> m = Message()
>>> m.message_type_id = 1
>>> m.text="aergaer"
>>> m.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 2479, in save
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1775, in execute
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1470, in _execute
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1885, in execute_sql
  File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1871, in sql_error_handler
sqlite3.IntegrityError: message.message_type_id may not be NULL


有人知道我该怎么做吗?

最佳答案

我很好奇为什么要使用整数值,ORM的好处是能够处理对象...

但是,如果您愿意,可以:

m = Message()
m.message_type = 1
m.text = 'whateve

关于python - 如何在Python Peewee中手动插入ForeignKeyFields?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21579137/

10-14 09:31