Django を始めたばかりで、テスト用のアプリケーションの作成を開始しました。ただし、ローラーコースターを ManyToMany に指定し、管理 UI を介してパークのインスタンスを追加すると、次のエラーが発生します: IntreityError, rollercoasters_id may not be NULL
私はこれに関する本を完成させたばかりで、コードは本のように多かれ少なかれ正確です (コードは本の中で機能します)。
私は何を間違っていますか?
ManyToMany の代わりに ForeignKey がある場合、アプリケーションは機能します
from django.db import models
class Company(models.Model):
name=models.CharField(max_length=30)
location=models.CharField(max_length=30)
website=models.URLField()
def __unicode__(self):
return self.name
class Constructor(models.Model):
name=models.CharField(max_length=30)
location=models.CharField(max_length=30)
contactnumber=models.CharField(max_length=30)
website=models.URLField()
def __unicode__(self):
return self.name
class RollerCoasters(models.Model):
name=models.CharField(max_length=30)
dateopenned=models.DateField
type=models.CharField(max_length=30)
builtby=models.ForeignKey(Constructor)
dateopenned=models.DateField()
def __unicode__(self):
return self.name
class Parks (models.Model):
name=models.CharField(max_length=30)
parent=models.ForeignKey(Company)
location=models.CharField(max_length=1000)
rollercoasters=models.ManyToManyField(RollerCoasters)
dateopenned=models.DateField()
def __unicode__(self):
return self.name
エラー:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/Parks/parks/add/
Django Version: 1.4.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'Parks',
'django.contrib.admin',
'django.contrib.admindocs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
366. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
196. return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\db\transaction.py" in inner
209. return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
955. self.save_model(request, new_object, form, False)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in save_model
709. obj.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
463. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
551. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in _insert
203. return insert_query(self.model, objs, fields, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in insert_query
1593. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
910. cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
40. return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
344. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /admin/Parks/parks/add/
Exception Value: Parks_parks.rollercoasters_id may not be NULL