Code

Opened 15 months ago

Closed 14 months ago

Last modified 14 months 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

Attachments (0)

Change History (7)

comment:1 Changed 15 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

comment:2 Changed 14 months ago by phantummm

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 Changed 14 months ago by phantummm <phantummm@…>

  • Owner changed from nobody to anonymous
  • Status changed from new to assigned

comment:4 Changed 14 months ago by phantummm <phantummm@…>

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

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

In a7358586e95016e414d90bccdff4675b1b65bbe3:

Merge pull request #730 from phantummm/ticket_19719

Fixed #19719 - Removed misleading example from ModelForm documentation

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

In 5c3c8bf09a986660fcd4421df38745167a10ba11:

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

Backport of 976dc07baf from master

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

In 3d6388941d231e7ae1b5a53cdf2e401b704744b5:

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

Backport of 976dc07baf from master

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.