Opened 3 months ago

Closed 3 months ago

#35746 closed Cleanup/optimization (needsinfo)

make the form.cleaned_data attribute of form available in the __init__

Reported by: piscvau Owned by:
Component: Forms Version: 5.0
Severity: Normal Keywords: clean_field_name, form.full_clean
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Sarah Boyce)

Hello
form.clean_field_name functions have no parameter passed to it as indicated in the documentation and need to access the form.cleaned_data dictionary. However this cleaned_data dictionary is not created in the __init__ of the form but only in the full_clean method of the form.

the need:
in the form.__init__, add closure functions as clean_field_name functions and use the cleaned_data dictionary as a free variable of this closure function. However this is not possible because the cleaned_data_dictionary is not yet created.

Suggested patch :
create the cleaned_data.attribute in the __init__ instead of the full_clean

Change History (2)

comment:1 by Sarah Boyce, 3 months ago

Description: modified (diff)

comment:2 by Sarah Boyce, 3 months ago

Resolution: needsinfo
Status: newclosed

I think what you're trying to say is that you need to reference self.cleaned_data in clean_<fieldname>()

As per the docs, this should be possible: https://docs.djangoproject.com/en/5.1/ref/forms/validation/#form-and-field-validation

The clean_<fieldname>() method is called on a form subclass – where <fieldname> is replaced with the name of the form field attribute. This method does any cleaning that is specific to that particular attribute, unrelated to the type of field that it is. This method is not passed any parameters. You will need to look up the value of the field in self.cleaned_data and remember that it will be a Python object at this point, not the original string submitted in the form (it will be in cleaned_data because the general field clean() method, above, has already cleaned the data once).

Feel free to write a test/share some code if you think you've found a bug here

Note: See TracTickets for help on using tickets.
Back to Top