[Answer]-Pre-populating FormPreview for editing data

1👍

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),
                              )
👤Trewq

Leave a comment