django.db.utils.IntegrityError:NOT NULL约束失败:chat_chat2.message

时间:2017-06-24 04:11:04

标签: django django-models django-views constraints notnull

我尝试使用jquery在django中构建基本的聊天应用程序。在我对views.py的发布视图中,我成功获取了消息数据,但是在c.save()中我得到了以下错误。我也尝试调试传递默认值但得到相同的错误。我无法将其作为非空约束。

models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Chat2(models.Model):
    user = models.ForeignKey(User)  
    message = models.CharField(max_length=500)

    def __unicode__(self):
        return self.message

chat.js

    $('#msg_f').on('submit',function(event) {
    event.preventDefault();
    $.ajax({
        datatype:"json",
        url : '/login/post/',
        type: 'POST',
        data:{msgbox : $('#msg_box').val()},
        success : function(data){
            $('#msg_box').val('');
            $('#msg_b').append('<div class="msg_right"><p class="msg">'+data.msgbox+'</p></div>');  
        }
    });
});

views.py

 def post(request):
    if request.method=="POST":
        msg = request.POST.get('msgbox',None)
        c = Chat2(user=request.user,message=msg)
        if msg != '':
            c.save()
        return JsonResponse({'msg':msg,'user':c.user.username})
    else:
        return HttpResponse("request must be post")

错误

[24/Jun/2017 09:21:45] "GET /login/login/chat_home/ HTTP/1.1" 200 3216
Internal Server Error: /login/post/
Traceback (most recent call last):
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: chat_chat2.message

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\django-tuto\login\chat\views.py", line 40, in post
    c.save()
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\models\base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\models\base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\models\base.py", line 922, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\models\base.py", line 961, in _do_insert
    using=using, raw=raw)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\models\query.py", line 1063, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Harshit\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: chat_chat2.message

0 个答案:

没有答案