插入数据库MySQL Django

时间:2013-07-18 22:39:48

标签: django django-models

我的数据未保存在数据库中。

这是我的 views.py 文件

def rr_reply(request):


    cc_user = request.user.id
    conversation_id = request.GET.get('conversation_id','')
    b_id = request.GET.get('b_id','')


    if request.method == 'POST': 
        form = CcReply(request.POST) 
        if form.is_valid():
            date_time = datetime.datetime.now()
            reply = form.cleaned_data['reply']

            queries = WebQuery.objects.using('launchg').filter(conversation_id = conversation_id)

            for query in queries:
                q_id = query.query_id               

            cc_chat_data_obj = CcChatData(conversation_id = conversation_id , reply_from = request.user.id , 
                b_id = b_id, q_id = q_id)
            web_reply_obj = WebReply(query_id = q_id, conversation_id = conversation_id , b_id = b_id, 
                u_query = reply , date_time = date_time.strftime("%Y-%m-%d %H:%M:%S"),diff = 0)

            cc_chat_data_obj.save()
            web_reply_obj.save()


    else:
        form = CcReply()

2 个答案:

答案 0 :(得分:3)

试试这个:

 cc_chat_data_obj = CcChatData.objects.create(conversation_id = conversation_id , reply_from = request.user.id , b_id = b_id, q_id = q_id)
 web_reply_obj = WebReply.objects.create(query_id = q_id, conversation_id = conversation_id , b_id = b_id, u_query = reply , date_time = date_time.strftime("%Y-%m-%d %H:%M:%S"),diff = 0)

答案 1 :(得分:0)

您是否检查过您的表格是否确实有效? (is_valid)

当表单无效时,您当前的代码不执行任何操作。