Man将这些值从函数传递给db模型:
功能定义:
def send_as_notification_to(person , link_id , unique_content , which_app, notification_type ):
nc = NotificationContent.objects.create( link_id = link_id , unique_content = str(unique_content))
app_obj = MetaAppNames.objects.create(name = which_app)
nt = NotificationType.objects.create( type = notification_type , app = app_obj)
notification = Notification(person = person)
notification.content = nc
notification.notification_type = nt
notification.save()
nc.save()
app_obj.save()
nt.save()
致电观点:
def crave_form(request):
if request.method == 'POST':
form = IcraveForm(request.POST)
if form.is_valid():
crave = form.save(commit = False)
crave.person = request.user
send_as_notification_to('admin' ,crave.id , crave.person , 'icrave' , 'crave' )
crave.save()
else:
form = IcraveForm()
return render(request, 'icrave/form.html', { 'form' : form})
但它给我的错误是:
notification_notificationcontent.link_id可能不是NULL
Traceback ::
Environment:
Request Method: POST
Request URL: http://localhost:8000/icrave/create/
Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.comments',
'ec.kiosk',
'ec.chakra',
'ec.ajax',
'ec.broadcast',
'ec.connect',
'ec.seek',
'ec.feed',
'ec.ec_model',
'ec.info',
'ec.domains',
'ec.souk',
'ec.meta',
'ec.shastra',
'ec.chat',
'ec.log',
'ec.icrave',
'ec.notification',
'doj',
'django.contrib.admin']
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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Volumes/Disk2/workspace/ec/ec/icrave/views.py" in crave_form
15. send_as_notification_to('admin' ,crave.id , crave.person , 'icrave' , 'crave' )
File "/Volumes/Disk2/workspace/ec/ec/notification/api.py" in send_as_notification_to
6. nc = NotificationContent.objects.create( link_id = link_id , unique_content = str(unique_content))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py" in create
138. return self.get_query_set().create(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in create
360. obj.save(force_insert=True, using=self.db)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py" in save
460. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py" in save_base
553. result = manager._insert(values, return_id=update_pk, using=using)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
195. return insert_query(self.model, values, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
1436. return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
791. cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
735. cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/util.py" in execute
34. return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
234. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /icrave/create/
Exception Value: notification_notificationcontent.link_id may not be NULL
答案 0 :(得分:0)
调用函数时,尚未将对象(crave
)提交到数据库。如果它有id
属性的数据库生成的标识符,则它仍然为null。这是id
属性值,它正试图用于它抱怨的link_id
。
在致电您的功能之前,您需要停止指定commit=False
,或者需要致电crave.save
。
答案 1 :(得分:0)
commit=False
表示您尚未保存任何内容,这意味着该模型尚未拥有有效ID。首先保存为真实,然后传递更新模型的ID。