简体   繁体   English

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

[英]Django write in database when form is submit

i want to use html form to be able to send back information to my view.py, the goal is to get the data, use it as arguments in a call to a stored procedure. 我想使用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} )

The stored procedure is supposed to create a new entry. 该存储过程应该创建一个新条目。 What i want to do, but i'm not sure if possible would be : 我想做什么,但我不确定是否可能是:

Fill form => retrieve data in view => call stored procedure with the retrieved data => get the ID of the new entry so the user can be redirected to a another view that take the id in url parameters. 填写表单=>在视图中检索数据=>使用检索到的数据=>调用存储过程=>获取新条目的ID,以便可以将用户重定向到另一个使用url参数作为ID的视图。

Would this be possible to do ? 这可能吗?

Edit : I managed to get the post request working aswell as my stored procedure, my problem is now for the last part, redirecting the user on the right page after submiting the form. 编辑:我设法使发布请求以及存储过程正常工作,我的问题现在是最后一部分,在提交表单后在右侧页面上重定向用户。

the current page is /gestion_mouvement/mouvementCreation and i want the user to be redirected on /gestion_mouvement/mouvementDetails/{{ID}} 当前页面是/ gestion_mouvement / mouvementCreation,我希望在/ gestion_mouvement / mouvementDetails / {{ID}}上重定向用户

Problem is it seem that the query is too slow, because by the time i submit the form the user gets redirected to /gestion_mouvement/mouvementDetails/ and not recieving the ID. 问题是查询似乎太慢了,因为到我提交表单时,用户被重定向到了/ gestion_mouvement / mouvementDetails /,而没有收到ID。

What about creating a new cursor to fetch your last id created? 如何创建一个新的游标以获取您最近创建的ID?

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

I've had the same issue recenly. 我最近也遇到过同样的问题。 Here is what I did. 这是我所做的。 Query concerned object model just after insert (after form.save()) and get the max(id) generated 插入后(在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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM