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?


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,exclude='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