tags:

views:

1595

answers:

1
class RegistrationFormPreview(FormPreview):
    preview_template    = 'workshops/workshop_register_preview.html'
    form_template       = 'workshops/workshop_register_form.html'

    def done(self, request, cleaned_data):
        # Do something with the cleaned_data, then redirect
        # to a "success" page. 
        # data = request.POST.copy()
        # data['user_id'] = u'%s' % (request.user.id)
        # cleaned_data['user'] = u'%s' % (request.user.id)
        #f = self.form(cleaned_data)
        #f = self.form(data)
        #f.user = request.user


        f = self.form(request.POST)
        f.save()

        pdb.set_trace()
        return HttpResponseRedirect('/register/success')

As you can see, I've tried a few ways, and that have been commented out. The task is apparently simple: Add the user from request to the form before save, and then save.

What is the accepted, working method here?

+2  A: 

If the user can't be modified, I would say it shouldn't even be included in the form in the first place.

Either way, using the commit argument to prevent the resulting object being saved immediately should work (assuming FormPreview uses ModelForm):

obj = form.save(commit=False)
obj.user = request.user
obj.save()
insin
Thanks insin, right on!I have a further related question if you'd like to pursue: http://stackoverflow.com/questions/628132/django-form-preview-how-to-work-with-cleaneddatabest thx.
Antonius Common
@insin What happens if an error is raised by `obj.save()`? This might not be the case most of the time with a simple `request.user` case, but what about more complicated cases. How do you handle validation and error rendering after `save(commit=False)`
orokusaki