Discussion:
formsets and empty results
Wim Feijen
2010-05-15 17:34:04 UTC
Permalink
Hi all,

Can some of you please help me with the following?

For an application, I need to display the same form ten times over.
Filling in each form is optional. Of course, I'd like to keep this
safe & simple and therefore I am looking at formsets.

Is it possible to use a formset, call formset.is_valid() and that it
ignores empty forms?

I'm using the forms only for adding new data, not for altering
existing data.

Thanks!

Best regards, Wim
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Dexter
2010-05-15 18:31:29 UTC
Permalink
Hey,

Ik zou deze pagina even bekijken:
http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-validation

<http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-validation>
Grtz
Post by Wim Feijen
Hi all,
Can some of you please help me with the following?
For an application, I need to display the same form ten times over.
Filling in each form is optional. Of course, I'd like to keep this
safe & simple and therefore I am looking at formsets.
Is it possible to use a formset, call formset.is_valid() and that it
ignores empty forms?
I'm using the forms only for adding new data, not for altering
existing data.
Thanks!
Best regards, Wim
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Wim Feijen
2010-05-17 10:01:34 UTC
Permalink
Hi Dexter, thanks for your answer,

The urls you gave are about forms, not formsets? My code looks like
this:

----

@teacher_required
def add_students(request):
school = School.objects.for_user(request.user)

class StudentForm(UserCreationForm):
name = forms.CharField(label='Naam')
group = forms.ChoiceField(label='Groep')
def __init__(self, *args, **kwargs):
super(StudentForm, self).__init__(*args, **kwargs)
self.fields['group'].choices = [(g.id, g.name) for g in
StudentGroup.objects.for_school(school)]

from django.forms.formsets import formset_factory
StudentFormSet = formset_factory(StudentForm, extra=10)
if request.method == 'POST':
formset = StudentFormSet(request.POST)
if formset.is_valid():
for form in formset:
if form.is_valid():
user = form.save()
student = Student(user=user,
name=form.cleaned_data['name'], group_id=form.cleaned_data['group'])
student.save()
return HttpResponseRedirect(reverse('schooladmin_students'))
else: #GET
formset = StudentFormSet()

base_template = get_base_template(request.user)
return render_to_response('school/add_students.html', {
'formset': formset,
'base_template': base_template
}, context_instance=RequestContext(request))

---
The StudentForm is defined over here because I could not set the
groups otherwise. I am using Django 1.0 . After the post, I'd like to
ignore empty forms in the formset.

Is this possible?

Best regards, groeten van Wim
Hey,
Ik zou deze pagina even bekijken:http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-...
<http://docs.djangoproject.com/en/1.1/ref/forms/validation/#ref-forms-...>
Grtz
Post by Wim Feijen
Hi all,
Can some of you please help me with the following?
For an application, I need to display the same form ten times over.
Filling in each form is optional. Of course, I'd like to keep this
safe & simple and therefore I am looking at formsets.
Is it possible to use a formset, call formset.is_valid() and that it
ignores empty forms?
I'm using the forms only for adding new data, not for altering
existing data.
Thanks!
Best regards, Wim
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com.
To unsubscribe from this group, send email to django-users+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Loading...