Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19719 closed Bug (fixed)

Example in documentation saves an unbounded form

Reported by: alexpirine Owned by: anonymous
Component: Documentation Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

This doesn't work:

>>> a = Article.objects.get(pk=1)
>>> f = ArticleForm(instance=a)
>>> f.save() # AttributeError: 'Article' object has no attribute 'cleaned_data'

Also confirmed here:

http://stackoverflow.com/questions/2393348/django-form-error#comment2374980_2394120

and here:

https://docs.djangoproject.com/en/dev/ref/forms/api/#behavior-of-unbound-forms

Change History (7)

comment:1 by Claude Paroz, 12 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by phantummm, 12 years ago

The issue here seems to be that whoever wrote the example assumed, reasonably enough perhaps, that "instance=a" binds the form. But it appears that the given instance and its fields are used only for supplying the "initial" parameter to the BaseForm class, whereas BaseForm.is_bound is based only on the presence of the "data" and "files" parameters. This is why, looking at the example above:

>>> f = ArticleForm({'title':'...', 'body':'...'})
>>> f.save()
<Article: Article object>

...would work, saving the passed data as a new Article and binding it to the form.

comment:3 by phantummm <phantummm@…>, 12 years ago

Owner: changed from nobody to anonymous
Status: newassigned

comment:4 by phantummm <phantummm@…>, 12 years ago

comment:5 by Tim Graham <timograham@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In a7358586e95016e414d90bccdff4675b1b65bbe3:

Merge pull request #730 from phantummm/ticket_19719

Fixed #19719 - Removed misleading example from ModelForm documentation

comment:6 by Tim Graham <timograham@…>, 12 years ago

In 5c3c8bf09a986660fcd4421df38745167a10ba11:

[1.5.x] Fixed #19719 - Removed misleading example from ModelForm documentation

Backport of 976dc07baf from master

comment:7 by Tim Graham <timograham@…>, 12 years ago

In 3d6388941d231e7ae1b5a53cdf2e401b704744b5:

[1.4.x] Fixed #19719 - Removed misleading example from ModelForm documentation

Backport of 976dc07baf from master

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