我是Django的新手,但看起来这个应该很容易。我正在使用Django 1.5 beta2,但我认为它应该足够稳定,只需要这么简单。 这是我在注册应用程序中的课程。
class Shift(models.Model):
begin_date = models.DateTimeField()
finish_date = models.DateTimeField(null=True)
def __unicode__(self):
return str(self.begin_date) + ' to ' + str(self.end_date)
class Transaction(models.Model):
begin_date = models.DateTimeField()
finish_date = models.DateTimeField(null=True)
status = models.CharField(max_length=10, default='Started')
shift = models.ForeignKey(Shift)
def __unicode__(self):
return str(self.begin_date) + ' to ' + self(self.end_date)
但是当我尝试创建子事务时,它失败了。
david@fink:~/orthosie$ python manage.py shell
Python 2.7.3 (default, Sep 26 2012, 21:53:58)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from register.models import Shift, Transaction
>>> from django.utils import timezone
>>> s = Shift(begin_date=timezone.now())
>>> s.transaction_set.create(begin_date=timezone.now())
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 529, in create
return super(RelatedManager, self.db_manager(db)).create(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 149, in create
return self.get_query_set().create(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 537, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 632, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1646, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 935, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 41, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 364, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 362, in execute
return Database.Cursor.execute(self, query, params)
IntegrityError: register_transaction.shift_id may not be NULL
>>>
我使用sqliteman手动挖掘sqlite数据库,可以看到表register_transaction实际上有一个shift_id列。
-- Describe REGISTER_TRANSACTION
CREATE TABLE "register_transaction" (
"id" integer NOT NULL PRIMARY KEY,
"begin_date" datetime NOT NULL,
"finish_date" datetime,
"status" varchar(10) NOT NULL,
"shift_id" integer NOT NULL REFERENCES "register_shift" ("id")
)
我觉得必须有一些我想念的小事。
答案 0 :(得分:3)
s
没有ID,因为它尚未保存。
在拨打电话之前拨打s.save()
,或使用Shift.objects.create
代替Shift()
。