Вопрос

I'm using the Python Peewee ORM and I wonder how I can insert a foreign field by simply inserting the key, instead of an object itself. For example, I've got a model called message with a message_type, which is a ForeignKeyField. I know that the key to the message_type is 1, so I try this:

>>> 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

Does anybody know how I can do this?

Это было полезно?

Решение

I'm curious why you're using integer values, the benefit of an ORM is to be able to work with objects...

But, if you want you can:

m = Message()
m.message_type = 1
m.text = 'whateve
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top