Is this the right way to open object from DB for editing in the same form, and then saving the updated value for the same object using django?

This is my code:

To create the new issue object, use form

def raise_issue_view(request, *args, **kwargs):
    
    form = RaiseIssueForm(request.POST or None)
    if form.is_valid():
        form.save()

        obj = RaiseIssueModel.objects.latest('id')
        return redirect("/raise_issue/" + str(obj.id))
    
    context = {
        "form" : form
    }

    return render(request,"raise_issue.html", context)

To edit the previously created issue, by link 127...8000/edit_issue/<issue_id>

def edit_issue_view(request, id):
    obj = RaiseIssueModel.objects.get(id=id)

    form = RaiseIssueForm(instance=obj)
    new_form = RaiseIssueForm(request.POST, instance=obj)
    

    if new_form.is_valid():
        
        new_form.save()
        
        return redirect("/raise_issue/" + str(obj.id))
    
    context = {
        "form" : form
    }

    return render(request,"raise_issue.html", context)

Here, in Edit issue view, first i'm loading the DB data into 'form', then I'm creating a new form (new_form) to save the updated data. Is this OK, or is there a better way to do this? Thanks,



Read more here: https://stackoverflow.com/questions/64889705/is-this-the-right-way-to-open-object-from-db-for-editing-in-the-same-form-and-t

Content Attribution

This content was originally published by Ankit Singh at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: