Opened 2 years ago

Closed 6 months ago

Last modified 6 months ago

#21894 closed Bug (fixed)

Error in example code in django.forms.Form.clean()¶

Reported by: richard.kellner@… Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In Documentation version: development explanation of Cleaning and validating fields (https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other) that depend on each other there is and error. Code in the sample would thow an exeptoion: TypeError: argument of type 'NoneType' is not iterable because function does not return anything back. Documentation in 1.6 is correct and does not have this error. To resolve add return statement at the end of both function in example:

# Always return the full collection of cleaned data.
return cleaned_data

Attachments (1)

21894.diff (1.1 KB) - added by timgraham 7 months ago.

Download all attachments as: .zip

Change History (7)

comment:1 follow-up: Changed 2 years ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

Hi,

The documentation is correct: this is a new feature in the development version of Django (soon-to-be 1.7), as described by the note under the example:

In previous versions of Django, form.clean() was required to return a dictionary of cleaned_data. This method may still return a dictionary of data to be used, but it’s no longer required.

comment:2 in reply to: ↑ 1 Changed 7 months ago by CarstenF

  • Resolution worksforme deleted
  • Status changed from closed to new

Replying to bmispelon:

Hi,

The documentation is correct: this is a new feature in the development version of Django (soon-to-be 1.7), as described by the note under the example:

In previous versions of Django, form.clean() was required to return a dictionary of cleaned_data. This method may still return a dictionary of data to be used, but it’s no longer required.

This however (referring to this example code), is inconsistent with the example code's statement

cleaned_data = super(ContactForm, self).clean()

about which a subsequent paragraph says:

Note that the call to super(ContactForm, self).clean() in the example code ensures that any validation logic in parent classes is maintained.

That is, if the clean() method of the parent class is coded like the example code for the child, the example code won't work.

Changed 7 months ago by timgraham

comment:3 Changed 7 months ago by timgraham

  • Has patch set
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 6 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 80855a4:

Fixed #21894 -- Corrected a form.clean() example in case a superclass doesn't return data.

comment:5 Changed 6 months ago by Tim Graham <timograham@…>

In 6861c20:

[1.8.x] Fixed #21894 -- Corrected a form.clean() example in case a superclass doesn't return data.

Backport of 80855a4b3787bace050a8b4a2b80f79306e69812 from master

comment:6 Changed 6 months ago by Tim Graham <timograham@…>

In 55ed23fd:

[1.9.x] Fixed #21894 -- Corrected a form.clean() example in case a superclass doesn't return data.

Backport of 80855a4b3787bace050a8b4a2b80f79306e69812 from master

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