首页 文章

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

提问于
浏览
0

我想使用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} )

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

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

这有可能吗?

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

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

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

2 回答

  • 0

    如何创建新游标来获取您创建的最后一个ID?

    cursor.execute("SELECT max(id) from MANUAL_PESEE")
    return {rec[0] for rec in cursor}
    
  • 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))
    

相关问题