Aditya Singh
Aditya Singh

Reputation: 970

object is none after saving modal form

i am using django forms for my product model but the instance is returned as None

if request.user.is_authenticated:
        if request.method =='POST':
            form = ProductModelForm(request.POST)
            if form.is_valid():
                instance = form.save()
                product = instance.save()
                print product
                return HttpResponseRedirect(reverse("DataEntry"))
            else:
                return HttpResponseRedirect(reverse("DataEntry"))

        else:
            form = ProductModelForm()

            context = {
            'form' : form , 
            'products' : Product.objects.all().order_by('-timestamp')
            }
            return render(request, "products/dataentry.html", context)

what might be the issue over here? need help

Upvotes: 1

Views: 364

Answers (1)

Alasdair
Alasdair

Reputation: 309069

Calling form.save() returns the instance. In your case, you are not using form.save(commit=False), so there is no need to save the instance.

Unlike form.save(), instance.save() returns None, so you are currently assigning None to product.

You can simplify the code to:

        if form.is_valid():
            product = form.save()
            print product

Upvotes: 1

Related Questions