Opened 19 years ago
Closed 17 years ago
#899 closed defect (fixed)
Formfield doesn't display default value
Reported by: | 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)
Change History (16)
comment:1 by , 19 years ago
Component: | Template system → Metasystem |
---|
comment:2 by , 19 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:3 by , 18 years ago
Component: | Metasystem → django.newforms |
---|---|
Has patch: | set |
Needs tests: | set |
Resolution: | wontfix |
Status: | closed → reopened |
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 , 18 years ago
Attachment: | initial_formfield.patch added |
---|
Use get_default model fields method to set initial values for forms.
comment:4 by , 18 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:5 by , 18 years ago
comment:6 by , 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 , 18 years ago
Triage Stage: | Ready for checkin → Accepted |
---|
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 , 18 years ago
Attachment: | django-use-model-default-as-newforms-initial.diff added |
---|
This should do it for current trunk (formfield() nicely refactored using super())
comment:8 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | reopened → new |
by , 17 years ago
same patch against trunk, but with the tests requested by Malcolm
comment:9 by , 17 years ago
Keywords: | form_for_model added |
---|---|
Needs tests: | unset |
Status: | new → assigned |
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 , 17 years ago
Keywords: | sprintsept14 added |
---|
comment:12 by , 17 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
A user on IRC reports that the patch works and tests pass, so marking ready for checkin.
comment:13 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
The formfields framework is decoupled from models.