Question

I have been able to successfully use the FormPreview in django 1.4 to preview before submitting for adding a new record in the Event model. Love the functionality.

Here is how I do this:

#urls.py
url(r'^addevent/', EventFormPreview(EventForm)),

#views.py 
class EventFormPreview(FormPreview):
  def done(self, request, cleaned_data):
    pdb.set_trace()
    new_event = Event(**cleaned_data)
    new_event.user = request.user
    new_event.save()
    return render_to_response("event/thanks.html",
                              {'cleandata': cleaned_data,},
                              context_instance=RequestContext(request),
                              )

Now, I want to have a similar editevent() in my view that goes through the same preview process and the final submit. The only difference will be that the form will be pre-populated with the data I am I trying to edit. How do I do this by writing minimal code and making use of the existing preview flow? Here is what I think the urls.py portion will look like:

 url(r'^(?i)editevent/(?P<id>\d+)/$', EditEventFormPreview(EventForm)),

I suspect I will have to redefine the init() in the EventFormPreview() and load the data there. Please let me know how to do this..

No correct solution

OTHER TIPS

For once I am glad no one responded to my question - forced me to learn about oop.

So here is how I got what I wanted, and I think it is good pythonic way of doing it.

I override the parse_params() to get the record id, and then override the get_initial() to populate the form with the data. Then in the done(), I bring up the old record, and update it with the cleaned_data.

class EditEventFormPreview(FormPreview):
def parse_params(self, *args, **kwargs):
    self.state["recordid"] = kwargs["id"]
    pass

def get_initial(self, request):
    ob = Event.objects.filter(pk=self.state["recordid"]).values()[0]
    return ob

def done(self, request, cleaned_data):
    new_event = Event.objects.get(pk=self.state["recordid"])
    for (key, value) in cleaned_data.items():
        setattr(new_event, key, value)
    new_event.user = request.user
    new_event.save()
    return render_to_response("event/thanks.html",
                              {'cleandata': cleaned_data,},
                              context_instance=RequestContext(request),
                              )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top