[Fixed]-Accessing a non model field in pre_save signal

1👍

It sounds as though you just want to add to your form a hidden field, which you can then check if that has anything in it

class MyForm(Form):
    my_field = forms.CharField(widget=forms.HiddenInput())

    def clean_my_field(self):

        data = self.cleaned_data['my_field']

        if data:
             raise ValidationError()
        return data
👤Sayse

0👍

Answer: I was thinking that I could access FORM from pre_save signal. As explained above by contributors, this is NOT the case. You can only access model data with relation to form. So in my case, a field added only to the html form would now show up in pre_save instance.

I resolved the problem by creating a custom middleware that would trigger on any POST method, and check the presence and content of the decoupled html form field.

Leave a comment