Struggling with formsets

15 views
Skip to first unread message

Werner Brand

unread,
Feb 19, 2015, 3:25:00 PM2/19/15
to django...@googlegroups.com
I cannot seem to figure this, although I suspects it is really elementary:

I have two models:

class Employee(models.Model):
id_number = models.CharField(max_length=13)
surname = models.CharField(max_length=100)
name =  models.CharField(max_length=100)

class Payslip(models.Model):
surname = models.CharField(max_length=100)
name =  models.CharField(max_length=100)
salary = models.DecimalField(max_digits=20, decimal_places=2)

In my forms:

class PayslipForm(ModelForm):
class Meta:
model = Payslip
fields = ['surname', 'name', 'salary']

PayslipFormSet = modelformset_factory(Payslip, extra=0)


In views.py:

def Payslip(request):
employee = Employee.objects.all()
formset = PayslipFormSet(queryset=employee)
context = {'formset': formset}
return render(request, 'file.html', context)

def PayslipSubmit(request):
f = PayslipFormSet(request.POST)
if f.is_valid():
f.save()
return HttpResponse('Submitted')
else:
return HttpResponse(f.errors) 

The problem is that when I submit I get the following each form in formset:
  • id
    • Select a valid choice. That choice is not one of the available choices.
I hope someone can help!
 
 
 
 
 
 
 
 
 
 
 
 
 
 

aRkadeFR

unread,
Feb 19, 2015, 5:19:05 PM2/19/15
to django...@googlegroups.com
Hello,

Can we have the template?

Did you rendered the hidden fields id of your forms ?

Dump (by printing) the request.POST informations to see
if you received any "id" :)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8c766350-f426-475c-aac5-15f42952ce00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Werner Brand

unread,
Feb 20, 2015, 5:47:11 AM2/20/15
to django...@googlegroups.com
Thanks for the reponse! My template looks like this:

<form action="PayslipSubmit/" method="post">
{{ formset.management_form }}
<table>
{{ formset }}
</table>
{% csrf_token %}
<input type="submit" value="Submit">
</form>

Here is the print request.POST:

form-1-salaryform-MAX_NUM_FORMSform-0-surnameform-0-nameform-TOTAL_FORMSform-MIN_NUM_FORMSform-0-idform-1-nameform-INITIAL_FORMScsrfmiddlewaretokenform-0-salaryform-1-idform-1-surname

Thanks!

Reply all
Reply to author
Forward
0 new messages