1

我正在使用 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

有人知道我该怎么做吗?

4

1 回答 1

1

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

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

m = Message()
m.message_type = 1
m.text = 'whateve
于 2014-02-07T15:43:59.643 回答