Editing the Form in Django creates new instance

后端 未结 2 1982
既然无缘
既然无缘 2021-02-14 02:31

I am editing the form , it loads data correctly buy when i hit save it creates new entry in database.

Here is the view functions

def create_account(requ         


        
2条回答
  •  旧时难觅i
    2021-02-14 03:17

    Have you tried something like this?

     # Create a form to edit an existing Object.
         a = Account.objects.get(pk=1)
         f = AccountForm(instance=a)
         f.save()
    
    # Create a form to edit an existing Article, but use
    # POST data to populate the form.
        a = Article.objects.get(pk=1)
        f = ArticleForm(request.POST, instance=a)
        f.save()
    

提交回复
热议问题