I need a view to edit Enrollment objects for a particular Offering.

An Offering has a foreign key to a Room and a Course.  All three of
these (Offering, Room, and Course) have a maximum capacity field which
can be null (for unlimited).  Offering has a method called
get_max_capacity() which returns the minimum of all 3 maximums or None
if none have them defined.

An Enrollment has a foreign key to User and Offering.

I need a per-offering view of enrollments.  Where the number of forms
is the maximum capacity of the Offering or 3 extra if not defined.  I
would like to edit all fields (except for the foreign key to Offering
since this view is offering specific).

I am having the following problems trying to make the view Offering
specific.

1) Trying to use exclude=('offering',) since all Enrollments in this
view should be for the given offering.

2) Trying to make the view offering specific by using
queryset=offering.enrollment_set.all().

Without 1) or 2) I am able to edit existing Enrollments.  I am able to
create new Enrollments using the empty forms.

When I have exclude=('offering',) I cannot make use of the extra forms
because it will complain about no offering_id being set.

When I use queryset=offering.enrollment_set.all() and I submit, it
complains about all the empty forms not having information in them.

What am I doing wrong?

Here is my template and view code....


{% extends "train/base_admin.html" %}
{% block content %}
<h1>Enrollments for {{ offering }}</h1>
<form method="POST" action="">
    {{ formset.management_form }}
    {% for form in formset.forms %}
    <table>
        {{ form }}
    </table>
    <hr/>
    {% endfor %}
    <input type="submit" value="Submit" />
</form>
{% endblock %}


@login_required
def offering_admin(request, location_slug, offering_id):

    offering = get_object_or_404(Offering, id=offering_id)

    location = get_object_or_404(Location, slug=location_slug)

    # We use location in this view just so that urls look similar
    # we could get location from offering's room.  Still... make sure
    # the url is okay.
    if offering.room.location != location:
        return showmessage(request, 'Bad url.  Offering / Location
missmatch')

    if not request.user.profile.gid in settings.TRAIN_ADMINS
[location.name]:
        return showmessage(request, 'You are not an admin for location
%s' % location.long_name)

    try:
        extra = int(request.REQUEST.get('extra'))
    except:
        if offering.get_max_capacity():
            extra = offering.get_max_capacity() -
offering.enrollment_set.count()
        else:
            extra = 3

    EnrollmentFormSet = modelformset_factory(Enrollment, extra=extra,
exclude=('offering',))

    if request.method == 'POST':
        formset = EnrollmentFormSet(request.POST, request.FILES)
        if formset.is_valid():
            print 'VALID!!!'
            # do something with the formset.cleaned_data
            formset.save()
            # calling save may have populated variables (like cost
center when left blank)
            # so get it again from the database rather than the
request
            #formset = EnrollmentFormSet
(queryset=offering.enrollment_set.all())
    else:
        #formset = EnrollmentFormSet
(queryset=offering.enrollment_set.all())
        formset = EnrollmentFormSet()

    return render_to_response(
        'train/offering_admin_form.html',
        {'offering': offering,
         'formset' : formset,},
        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-users@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