Search code examples
pythondjangoinline-formset

Django Inline formsets always invalid


I am struck with inline formsets in Django. Each time I submit the formset it is considered invalid and hence the data doesn't get saved. I am a newbie in full-stack development and am learning Django. I am following Dennis Ivy's playlist on youtube and coding along with him. It is kind of an E-commerce portal where people can order stuff. Here is a link to the video where he implements inline formsets Video. It was working fine when I accepted orders through normal form i.e. one order at a time. I have done exactly as he did in his video. I also tried consulting my friends who are learning with me and also asked my mentor about it but no one could figure out what is wrong here. I browsed through the documentation but that too didn't help. Please help me out in fixing this. Github Repo link

views.py

from django.forms import inlineformset_factory
def createOrder(request, pkCrteOrder):
OrderFormSet = inlineformset_factory(customer, order, fields=('product','status'), extra=7)
CustomerOrderOwner = customer.objects.get(id=pkCrteOrder)
formSet = OrderFormSet(queryset = order.objects.none(), instance=CustomerOrderOwner)
if(request.method == 'POST'): 
    print("post request detected")
    formSet = OrderFormSet(request.POST, instance=CustomerOrderOwner)
    if formSet.is_valid():
        formSet.save()
        return(redirect('/'))
    else:
        print("formset was invalid")

context = {'formSet': formSet}
return(render(request, 'accounts/orderForm.html', context))

Brief explaination: From a particular customer's page we can place order. So pkCrteOrder represents the id of customer. I fetch the customer and pass it as instance for the formset and then send the formset to template. If form is submitted I check if it's valid and then save else i print "formset was invalid"

order model:

class order(models.Model):
STATUS = (
    ('Pending', 'Pending'),
    ('Out for delivery', 'Out for delivery'),
    ('Delivered', 'Delivered'),
)
customer = models.ForeignKey(customer, null = True, on_delete=models.SET_NULL)
product = models.ForeignKey(products, null = True, on_delete=models.SET_NULL)
date_created = models.DateTimeField(auto_now_add = True, null=True)
status = models.CharField(max_length=200, null=True, choices=STATUS)

template:

{% extends 'accounts/base.html' %} {% load static %} {% block content %}
<div class="row">
 <div class="col-md-6">
  <div class="card card-body">
   <form method="POST" action="">
    {% csrf_token %} 
    {{formset.management_form}} 
    {% for form in formSet %}
    {{form}}
    <hr />
    {% endfor %}
    <input type="submit" value="Submit" />
   </form>
  </div>
 </div>
</div>
{% endblock content %}

Thank you


Solution

  • In your template you have {{formset.management_form}}. But really the variable containing the forms you are sending from the view is called fromSet (notice the capital S). So change that code into {{formSet.management_form}} and it should work fine.