djangodjango-modelsdjango-forms

One project my obj.save() returning object how i did not used commit=False


Below code you will see that uf.save() statement returning the object how ? i did not use uf.save(commit=False)

def wash(request):
    if request.method == 'POST':
        uf = Userform(request.POST)
        wf = Profileform(request.POST,request.FILES)
        if uf.is_valid() and wf.is_valid():
            user = uf.save()
            print(user)
            wf.instance.user = user
            wf.save()
            return HttpResponse("suscess fully crated ")
        return render(request, 'webpage/washer.html', {'wf': wf, 'uf': uf})

Solution

  • When you call .save(…) [Django-doc] on a ModelForm, it will return the instance wrapped in the form, regardless whether commit=True (this is the case if you do not specify the commit=… parameter), or commit=False.

    This is often used, for example like in this case where we use the instance of one form, to use this when we work with the second form.