私は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")
)
私が見逃している小さな何かがあるに違いないと感じています。