2011-05-13 94 views
0

人正在從函數傳遞這些值到數據庫模型:完整性錯誤

函數定義:

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

回溯::

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

當你打電話給你的功能,你有沒有犯你的對象(crave)到數據庫中。如果它具有id屬性的數據庫生成的標識符,則它仍然爲空。而這正是它試圖用於link_id它所抱怨的id屬性值。

在調用函數之前,您需要停止指定commit=False,或者您需要致電crave.save

0

commit=False意味着你還沒有保存任何東西,這意味着該模型還沒有一個有效的ID。首先保存,然後傳遞更新模型的ID。