[Django]-Get selected value from Django ModelChoiceField form

2πŸ‘

βœ…

I need to pass the data from the AddressForm to the model Address object to call the .save method (?)

Then you probably should be just using a model form

class AddressForm(forms.Form):
     class Meta:
          model = Address

You can then just call form.save() and let django do the magic for you.

Either way, you should be calling is_valid prior to trying to access any data. This exposes the form’s cleaned_data dictionary which contains all the forms values post-validation.

πŸ‘€Sayse

4πŸ‘

The way you get any field value: by calling is_valid() and then form.cleaned_data[fieldname].

Leave a comment