Override values ​​in Django ModelForm instance during get_form ()

I have a "SavedSearch" model with a generic UpdateView. Each site user can have a saved search. When I start a search on the site, I want to give them the option to click the Save This Search button, which will bring them into an edit form for their current SavedSearch model and replace their values ​​with those found in their search (saved in request.session.params ).

So, I'm trying to catch a bound form on the template path and replace some values ​​without saving so that the user can edit the options or change their mind and undo.

I tried to update 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 also tried submitting request

to the form __init__

via get_form_kwargs()

and then did pretty much the same as above in the init form. Both tactics do not change the form data.

I've also tried passing request

to get_form_kwargs()

and then to __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)

      

It also does not change the display of the form. I also tried to completely override the kwarg data, in get_form_kwargs()

, but then the form tries to immediately check for validity and any fields not included in the new errors data

.

If anyone can give a nudge in the right direction, I would be grateful.

+3


source to share


2 answers


This is quite tricky to understand - it would be helpful to get more details about the user flow.

But I think you want to provide the original data for the form, not modify the field instances. This can be done by overriding get_initial

your view method to return a dictionary. This will be passed to the form returned from get_form

automatically, so there is no need to override this method.



def get_initial(self):
    if self.request.GET.get('ehf_override') and self.request.session.get('params'):
        initial_data = {}
        ...
    else:
        initial_data = super(SavedSearchUpdateView, self).get_initial()
    return initial_data

      

0


source


I believe that the correct method is to use self.object

for documentation in UpdateView to access the attributes of the object being edited. Then you can set the values ​​in the form like this:



def get_form(self, form_class=None):
    form = super(UpdateView, self).get_form(form_class)
    form.fields['location'].queryset = Location.objects.filter(foo=self.object.foo)
    return form

      

0


source







All Articles