Opened 19 years ago

Closed 17 years ago

#899 closed defect (fixed)

Formfield doesn't display default value

Reported by: ross@… Owned by: Philippe Raoult
Component: Forms Version: dev
Severity: normal Keywords: form_for_model, sprintsept14
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If my model contains this:

currency = meta.CharField(maxlength=4,default="Foo")

And a form template does this:

{{form.currency}}

I'd expect the resulting input widget to be pre-initialised to "Foo". However, it isn't.

Attachments (3)

initial_formfield.patch (6.3 KB ) - added by antisvin@… 18 years ago.
Use get_default model fields method to set initial values for forms.
django-use-model-default-as-newforms-initial.diff (1.1 KB ) - added by David Danier <goliath.mailinglist@…> 18 years ago.
This should do it for current trunk (formfield() nicely refactored using super())
899.diff (2.0 KB ) - added by Philippe Raoult 17 years ago.
same patch against trunk, but with the tests requested by Malcolm

Download all attachments as: .zip

Change History (16)

comment:1 by anonymous, 19 years ago

Component: Template systemMetasystem

comment:2 by Adrian Holovaty, 19 years ago

Resolution: wontfix
Status: newclosed

The formfields framework is decoupled from models.

comment:3 by antisvin@…, 18 years ago

Component: Metasystemdjango.newforms
Has patch: set
Needs tests: set
Resolution: wontfix
Status: closedreopened
Version: SVN

Now that there's formfield method especially for generating forms from model fields, initial values can be easily obtained from model.Obvious patch attached.

by antisvin@…, 18 years ago

Attachment: initial_formfield.patch added

Use get_default model fields method to set initial values for forms.

comment:4 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedReady for checkin

comment:5 by Brian Rosner <brosner@…>, 18 years ago

Marked #4019 as duplicate of this. It appears the patch on #4019 is essentially the same thing as this one.

comment:6 by David Danier <goliath.mailinglist@…>, 18 years ago

Sorry for the duplicate, I really didn't see this one year old ticket.

About the patches:
If I read ModelField.get_default() right this returns "" (empty string) as an last option, even if no default is provided. ModelField.get_default() actually generates some default if no default is provided. That's why I tested Field.has_default() in my code. Even if this has no real consequence in HTML-forms (value="" is the default that gets sent by the browser if no input is made), it only seemed unclean to me, because FormField.initial is different to ModelField.default, but it looks the same and may lead to confusion or mistakes (when working with the forms outside of some generic views/the admin).

comment:7 by Malcolm Tredinnick, 18 years ago

Triage Stage: Ready for checkinAccepted

This looks like the right approach for newforms, but it needs tests before it can be committed. Doesn't need to test every field, but it should show how the functionality works for a couple of examples so that we can verify we don't break it.

In particular, I would like to see that in the case of form_for_instance() constructing the form, we don't use the default values, but, rather, use the existing values from the instance attributes, whatever they may be. So having tests for a form_for_model() and a form_for_instance() case would be the minimu required here.

by David Danier <goliath.mailinglist@…>, 18 years ago

This should do it for current trunk (formfield() nicely refactored using super())

comment:8 by Philippe Raoult, 17 years ago

Owner: changed from nobody to Philippe Raoult
Status: reopenednew

by Philippe Raoult, 17 years ago

Attachment: 899.diff added

same patch against trunk, but with the tests requested by Malcolm

comment:9 by Philippe Raoult, 17 years ago

Keywords: form_for_model added
Needs tests: unset
Status: newassigned

I added regression tests to make sure we get the expected behavior in both form_for_model (default from the class's field) and form_for_instance (instance value).

comment:10 by George Vilches <gav@…>, 17 years ago

Keywords: sprintsept14 added

comment:11 by Philippe Raoult, 17 years ago

#4967 is a duplicate, closed in favor of this one

comment:12 by James Bennett, 17 years ago

Triage Stage: AcceptedReady for checkin

A user on IRC reports that the patch works and tests pass, so marking ready for checkin.

comment:13 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: assignedclosed

(In [6568]) Fixed #899 -- Use model field default values as formfield initial values in
form_for_model(). Patch from David Danier and PhiR. Thanks.

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