[Django] #35746: make the form.cleaned_data attribute of form available in the __init__

10 views
Skip to first unread message

Django

unread,
Sep 9, 2024, 5:41:37 AM9/9/24
to django-...@googlegroups.com
#35746: make the form.cleaned_data attribute of form available in the __init__
-------------------------------------+-------------------------------------
Reporter: piscvau | Type:
| Cleanup/optimization
Status: new | Component: Forms
Version: 5.0 | Severity: Normal
Keywords: clean_field_name , | Triage Stage:
form.full_clean | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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
dictionnary. However this cleaned_data dictionnary 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 dictionnary as a free variable of this closure
function. However this is not possible because the
cleaned_data_dictionnary is not yet created.

Suggested patch :
create the cleaned_data.attribute in the __init__ instead of the
full_clean
--
Ticket URL: <https://code.djangoproject.com/ticket/35746>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 9, 2024, 7:56:29 AM9/9/24
to django-...@googlegroups.com
#35746: make the form.cleaned_data attribute of form available in the __init__
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 5.0
Severity: Normal | Resolution:
Keywords: clean_field_name , | Triage Stage:
form.full_clean | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Sarah Boyce:

Old description:

> 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
> dictionnary. However this cleaned_data dictionnary 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 dictionnary as a free variable of this closure
> function. However this is not possible because the
> cleaned_data_dictionnary is not yet created.
>
> Suggested patch :
> create the cleaned_data.attribute in the __init__ instead of the
> full_clean

New description:

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

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35746#comment:1>

Django

unread,
Sep 9, 2024, 8:03:24 AM9/9/24
to django-...@googlegroups.com
#35746: make the form.cleaned_data attribute of form available in the __init__
-------------------------------------+-------------------------------------
Reporter: piscvau | Owner: (none)
Type: | Status: closed
Cleanup/optimization |
Component: Forms | Version: 5.0
Severity: Normal | Resolution: needsinfo
Keywords: clean_field_name , | Triage Stage:
form.full_clean | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => needsinfo
* status: new => closed

Comment:

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
--
Ticket URL: <https://code.djangoproject.com/ticket/35746#comment:2>
Reply all
Reply to author
Forward
0 new messages