[Django]-'dict' object has no attribute 'create' / 'save'

3👍

You should use a ModelForm, which will define the fields automatically to match those on the model. Then you can call form.save() to create the instance directly from the form.

3👍

Daniel’s answer is more correct

if form.is_valid():
    form.save()

I’ll leave my original answer though, just for the documentation links.

The form.cleaned_data is a dictionary type, and doesn’t have the create() method. I assume you are trying to create a model for that data into your database?

Take a look at Can a dictionary be passed to django models on create? to create a model from that dictionary. An example may be:

cd = form.cleaned_data
themodel = Individu(**cd)
themodel.save()

Here is the documentation for processing the form data as well.

https://docs.djangoproject.com/en/dev/topics/forms/#processing-the-data-from-a-form

Good luck!

-2👍

If you just want the submitted data to be saved as a django ModelForm, use form.save().

see: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#s-the-save-method

This will already use cleaned/validated data.

You can use form.cleaned_data if you need to do sth. else than saving it with a model instance.

👤alex

Leave a comment