Override values in Django ModelForm instance during get_form() -
i have model "savedsearch" generic updateview. each site user can have 1 saved search. when running search on site, want give them ability click "save search" button take them edit form current savedsearch model , replace values ones search (saved in request.session.params).
so, i'm trying catch bound form on way template , replace values without saving, user has chance edit options or change mind , cancel.
i've tried updating instance fields in get_form():
def get_form(self, form_class): form = super(savedsearchupdateview, self).get_form(form_class) # if request coming search form "save" click, # replace form context data options search form. if self.request.get.get('ss_override') , 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 tried passing request
form's __init__
via get_form_kwargs()
, doing pretty same above within form's init. both tactics result in no change whatsoever form data.
i tried passing request
get_form_kwargs()
, in __init__
:
def __init__(self, *args, **kwargs): request = kwargs.pop('request') super(dreamhomepreferenceform, self).__init__(*args, **kwargs) # if request coming property search form "save" click, # replace form context data options search form. if request.get.get('ehf_override') , 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 yields no change in form display. tried override data kwarg completely, in get_form_kwargs()
, form tries validate right away , fields not in new data
show errors.
if can give push in right direction, i'd grateful.
this pretty hard understand - have been helpful have more details user flow.
but think want provide initial data form, rather changing field instances. can done overriding get_initial
method of view, return dictionary. passed in form returned get_form
automatically, there no need override method.
def get_initial(self): if self.request.get.get('ehf_override') , self.request.session.get('params'): initial_data = {} ... else: initial_data = super(savedsearchupdateview, self).get_initial() return initial_data
Comments
Post a Comment