I have a model "SavedSearch" with a generic UpdateView. Each site user can have one Saved Search. When running a search on the site, I want to give them the ability to click a "Save this Search" button that will take them to the edit form for their current SavedSearch model and replace their values with the ones from their search (saved in request.session.params).
So, I'm trying to catch the bound form on the way to the template and replace some values without saving, so that the user has a chance to edit the options or change their mind and cancel.
I've tried updating the instance fields in get_form():
def get_form(self, form_class):
form = super(SavedSearchUpdateView, self).get_form(form_class)
# if request is coming from a search form "save" click,
# replace form context data with options from search form.
if self.request.GET.get('ss_override') and self.request.session.get('params', None):
params = self.request.session["params"]
if params.get('location', None): form.instance.location_keywords = params.get('location')
# ... etc.
return form
I've also tried passing request
to the form's __init__
via get_form_kwargs()
and then doing pretty much the same as above within the form's init. Both tactics result in no change whatsoever to the form data.
I also tried passing request
to get_form_kwargs()
and then in __init__
:
def __init__(self, *args, **kwargs):
request = kwargs.pop('request')
super(DreamHomePreferenceForm, self).__init__(*args, **kwargs)
# if request is coming from a property search form "save" click,
# replace form context data with options from search form.
if request.GET.get('ehf_override') and request.session.get('params', None):
params = request.session["params"]
new_data = {}
if params.get('location', None): new_data['location_keywords'] = params.get('location')
self.data = self.data.update(new_data)
This also yields no change in the form display. I also tried to override the data kwarg completely, in get_form_kwargs()
, but then the form tries to validate right away and any fields not in the new data
show errors.
If anyone can give a push in the right direction, I'd be grateful.