Django Forms with get_or_create

前端 未结 5 963
清酒与你
清酒与你 2021-01-31 09:46

I am using Django ModelForms to create a form. I have my form set up and it is working ok.

form = MyForm(data=request.POST)

if form.is_valid():
    form.sav         


        
5条回答
  •  无人共我
    2021-01-31 10:28

    You just need two cases in the view before the postback has occurred, something like

    if id:
        form = MyForm(instance=obj)
    else
        form = MyForm()
    

    then you can call form.save() in the postback and Django will take care of the rest.

提交回复
热议问题