Thursday, February 19, 2015

Struggling with formsets

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!
 
 
 
 
 
 
 
 
 
 
 
 
 
 

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment