[Fixed]-Can't save a form in Django (object has no attribute 'save')

21👍

save is available only for ModelForm by default, and not for forms.Form

What you need to do is this. Either use:

class AdvancedSearchForm(forms.ModelForm):
    valueofres = forms.ChoiceField (label="res", choices = ((0, 0),(2.2, 2.2)), required= False)
    class Meta:
        model=Search #or whatever object

Or:

def advancedsearch(request):
    if request.method == "POST":
        search_form = AdvancedSearchForm(request.POST, request.FILES)
        if search_form.is_valid():
            cd = search_form.cleaned_data
            search = #populate SearchObject()
            search.save()

9👍

Forms don’t have a save() method.

You need to use a ModelForm (docs) as that will then have a model associated with it and will know what to save where.

Alternatively you can keep your forms.Form but you’ll want to then extract the valid data from the for and do as you will with eh data.

if request.method == "POST":
    search_form = AdvancedSearchForm(request.POST, request.FILES)
    if search_form.is_valid():
        cd = search_form.cleaned_data

        search = Search(
            # Apply form data
        )
        search.save()

Leave a comment