views:

81

answers:

1

I'm trying to make a Django application to handle events. The view below handles the editing of already created events.

@login_required
def event_admin(request, event_id):
    event = get_object_or_404(Event, pk=event_id)

    if request.method == 'POST' and request.user == event.organiser:
        event_form = EventAdminForm(request.POST, instance=event)

        if event_form.is_valid():
            event_form.save()

            return HttpResponseRedirect(reverse('event_event_detail',
                args=(event.id, )))

    elif request.user == event.organiser:
        event_form = EventAdminForm(instance=event)

        return render_to_response('events/event_admin.html',
            {'event_form': event_form, 'event': event},
            context_instance = RequestContext(request))

    return HttpResponseRedirect(reverse('event_event_detail', args=(event.id, )))

When an existing event is edited the existing event is left untouched while a new event is created with the changes that were made. Can someone see what I am doing wrong? Many thanks.

Edit:

Here is the relevant section of forms.py if it helps.

class EventAdminForm(ModelForm):
    start_time = forms.DateTimeField(widget=SplitSelectDateTimeWidget(
        minute_step=5, second_step=60, years=range(2009, 2015)),
        initial=datetime.datetime.now())

    end_time = forms.DateTimeField(widget=SplitSelectDateTimeWidget(
        minute_step=5, second_step=60, years=range(2009, 2015)),
        initial=datetime.datetime.now())

    def clean(self):
        cleaned_data = self.cleaned_data
        start_time = cleaned_data.get('start_time')
        end_time = cleaned_data.get('end_time')

        # The start of an event cannot be after the end.
        if start_time > end_time:
            raise forms.ValidationError(u'The end of an event can not be before the start.')

        return cleaned_data

    class Meta:
        model = Event
        exclude = ('attendees', 'organiser', )
+2  A: 

when overriding clean() in modelforms u must give access to instance, so it lost the PK as i said in the comments and then the result of save() is an insert.

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

Overriding the clean() method¶

You can override the clean() method on a model form to provide additional validation in the same way you can on a normal form.

In this regard, model forms have two specific characteristics when compared to forms:

By default the clean() method validates the uniqueness of fields that are marked as unique, unique_together or unique_for_date|month|year on the model. Therefore, if you would like to override the clean() method and maintain the default validation, you must call the parent class's clean() method.

Also, a model form instance bound to a model object will contain a self.instance attribute that gives model form methods access to that specific model instance.

zalew
Thanks for your help. :) I managed to track down the problem to the custom date widget with this answer. Many thanks!
xoebus