当表单提交时,Django在数据库中写入

时间:2017-10-03 12:32:22

标签: python sql django forms

我想使用html表单将信息发送回我的view.py,目标是获取数据,在调用存储过程时将其用作参数。

def mouvementCreation(request):
    idMI = 0
    especes = TbEspece.objects.order_by('id')
    #Get Mouvement informations

    #Connection to 'erp-site' DB 
    cursor = connections['erp-site'].cursor()
    try:
        #Get Produits list from Espece
        query = "{CALL SP_webGET_PRODUIT_FROM_ESPECE(%s,%s,%s,%s,%s)}"
        arguments = (2016, 'C', 0, 10, 'A',)
        cursor.execute(query, arguments)
        produits = dictfetchall(cursor)

        #Get Transporters list
        cursor.execute("{CALL SP_webGET_TRANSPORT}")
        transporters = dictfetchall(cursor)

        #Get Livreur list
        cursor.execute("{CALL SP_webGET_LIVREUR}")
        livreurs = dictfetchall(cursor)
    finally:
        cursor.close()       

    cursor = connections['site'].cursor()
    try:
        #Get Circuit list
        cursor.execute("{CALL SP_webGET_CIRCUIT_FOR_MVT}")
        circuits = dictfetchall(cursor)

        #Get Source list
        cursor.execute("{CALL SP_webGET_SOURCE_FOR_MVT}")
        mvtsources = dictfetchall(cursor)

        #Get Dest list
        cursor.execute("{CALL SP_webGET_DEST_FOR_MVT}")
        destinations = dictfetchall(cursor)

        #Get PontBascule list
        cursor.execute("{CALL SP_webGET_PBASCULE}")
        pontBascules = dictfetchall(cursor)
    finally:
        cursor.close()

    reg_normes = TbRegauxnormes.objects.all()
    ordreexecs = TbOrdreexecution.objects.all()
    form = mouvementForm(request.POST or None)
    if form.is_valid():
        pont = form.cleaned_data['pont']
        dateheure = form.cleaned_data['dateheure']
        poid = form.cleaned_data['poid']
        dsd = form.cleaned_data['dsd']
        typepesee = form.cleaned_data['typepesee']
        #Connection to 'erp-site' DB 
        cursor = connections['pontbascule'].cursor()
        try:
            #Get Produits list from Espece
            query = "{CALL SP_ADD_MANUAL_PESEE(%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s)}"
            arguments = (pont, '11', dateheure, poid, dsd,typepesee, '','','','','','','','','','','','','','','','')
            cursor.execute(query, arguments)
        finally:
            cursor.close()  
    return render(request, 'mouvementCreation.html', {'form': form, 'especes' : especes, 'produits' : produits, 'transporters' :  transporters, 'livreurs' : livreurs, 'circuits' : circuits, 'mvtsources' : mvtsources, 'destinations' : destinations, 'pontBascules' : pontBascules} )

存储过程应该创建一个新条目。 我想做什么,但我不确定是否可能:

填写表格=>检索view =>中的数据使用检索到的数据调用存储过程=>获取新条目的ID,以便用户可以重定向到另一个在url参数中获取id的视图。

这可能吗?

编辑:我设法让邮件请求工作以及我的存储过程,我的问题现在是最后一部分,在提交表单后将用户重定向到右页。

当前页面是/ gestion_mouvement / mouvementCreation,我希望用户可以重定向到/ gestion_mouvement / mouvementDetails / {{ID}}

问题似乎是查询太慢了,因为当我提交表单时,用户被重定向到/ gestion_mouvement / mouvementDetails / 并没有收到身份证。

2 个答案:

答案 0 :(得分:0)

如何创建新游标来获取上次创建的ID?

cursor.execute("SELECT max(id) from MANUAL_PESEE")
return {rec[0] for rec in cursor}

答案 1 :(得分:0)

我有同样的问题。这就是我做的。 在插入之后(在form.save()之后)查询有关对象模型并获取生成的max(id)

form.save()
#mynewid = Msg.objects.all().order_by("-id")[0].id
mynewid = Msg.objects.latest('id').id
return redirect('/msg/{0}/edit/'.format(mynewid))