[Fixed]-Django form.save() not taking updated form.cleaned_data

1👍

Made an edit: I realise that instead of overriding save, we should instead clean/validate the phone number by using custom validation:

class ProfileForm(forms.ModelForm):
    office_phone = forms.CharField(max_length=20, label="Office Phone")  

    def clean_office_phone(self):
        value = self.cleaned_data.get("office_phone")
        try:
            value = value.replace(" ", "")
            response = validator.phone_numbers.get(str(value))

        except:
            raise ValidationError("The contact phone number you entered is invalid")

        return str(response.phone_number)

views.py:

if form.is_valid():
        form.save()
        success_message = "Your changes have been saved"

Leave a comment