On Dec 10, 9:05 am, cerberos <pe...@whywouldwe.com> wrote:
> I'm building a library system and have models Member, Loan and
> LoanItem (relationships as you'd expect), and an inline formset to
> enter a new loan (Loan and LoanItems).
>
> A member can borrow up to 6 books at a time, so my form contains 7
> individual forms (1 loan and 6 loan items), I need to get the member
> from the loan and the number of loan items from the loan items forms
> then raise an error if the items already loaned + the number of new
> loan items entered is greater than 6.
>
> I can raise errors based on the form or the formset but not both
> together, is this possible? What do I subclass to add the custom clean
> method?

checking whether an item is loaned is easy enough to do in the clean
method of an individual loanitem form.

but validation done with form combinations has to be done in the view
since a clean method can't span across different form/formset objects.

If you want to return the error as if it were a form error, you can
inject your error into the validated form by accessing its _errors
property - there are some examples here:

http://docs.djangoproject.com/en/dev/ref/forms/validation/#described-later

-Preston

Since a clean method only applies
>
> Here's my unfinished view
>
> def loan_add_form(request,template_name='resources/
> loan_add_form.html'):
>
>     loan = Loan()
>     LoanItemFormset = inlineformset_factory
> (Loan,LoanItem,formset=LoanItemBaseModelFormSet,can_delete=False,extra=6,ex 
> clude='date_returned')
>
>     if request.method == 'POST':
>
>         loan_form = forms.LoanForm(request.POST,instance=loan)
>         loan_item_formset = LoanItemFormset
> (request.POST,request.FILES,instance=loan)
>
>         if loan_form.is_valid() and loan_item_formset.is_valid():
>             loan_form.save()
>             loan_item_formset.save()
>
> #            request.user.message_set.create(message="Confirmation
> message.")
> #            return HttpResponseRedirect('/somepath')
>
>     else:
>         loan_form = forms.LoanForm(instance=loan)
>         loan_item_formset = LoanItemFormset(instance=loan)
>
>     context = {
>         'loan_form': loan_form,
>         'loan_item_formset': loan_item_formset,
>     }
>
>     return render_to_response(template_name, context,
> context_instance=RequestContext(request))

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to