Django

Code

Ticket #899 (closed: fixed)

Opened 3 years ago

Last modified 10 months ago

Formfield doesn't display default value

Reported by: ross@burtonini.com Assigned to: PhiR
Milestone: Component: Forms
Version: SVN Keywords: form_for_model, sprintsept14
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

initial_formfield.patch (6.3 kB) - added by antisvin@mail.ru on 03/29/07 05:21:31.
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@gmx.de> on 05/02/07 06:31:20.
This should do it for current trunk (formfield() nicely refactored using super())
899.diff (2.0 kB) - added by PhiR on 09/14/07 09:45:38.
same patch against trunk, but with the tests requested by Malcolm

Change History

11/27/05 06:45:08 changed by anonymous

  • component changed from Template system to Metasystem.

05/04/06 23:19:48 changed by adrian

  • status changed from new to closed.
  • resolution set to wontfix.

The formfields framework is decoupled from models.

03/29/07 05:21:26 changed by antisvin@mail.ru

  • status changed from closed to reopened.
  • component changed from Metasystem to django.newforms.
  • needs_tests set to 1.
  • version set to SVN.
  • has_patch set to 1.
  • resolution deleted.

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

03/29/07 05:21:31 changed by antisvin@mail.ru

  • attachment initial_formfield.patch added.

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

03/31/07 16:18:07 changed by SmileyChris

  • stage changed from Unreviewed to Ready for checkin.

04/13/07 01:10:52 changed by Brian Rosner <brosner@gmail.com>

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

04/13/07 02:23:38 changed by David Danier <goliath.mailinglist@gmx.de>

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).

04/25/07 06:44:14 changed by mtredinnick

  • stage changed from Ready for checkin to 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.

05/02/07 06:31:20 changed by David Danier <goliath.mailinglist@gmx.de>

  • attachment django-use-model-default-as-newforms-initial.diff added.

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

09/14/07 09:09:55 changed by PhiR

  • owner changed from nobody to PhiR.
  • status changed from reopened to new.

09/14/07 09:45:38 changed by PhiR

  • attachment 899.diff added.

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

09/14/07 09:47:34 changed by PhiR

  • keywords set to form_for_model.
  • status changed from new to assigned.
  • needs_tests deleted.

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).

09/14/07 10:12:44 changed by George Vilches <gav@thataddress.com>

  • keywords changed from form_for_model to form_for_model, sprintsept14.

09/14/07 11:19:37 changed by PhiR

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

09/16/07 04:55:43 changed by ubernostrum

  • stage changed from Accepted to Ready for checkin.

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

10/20/07 08:01:41 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

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


Add/Change #899 (Formfield doesn't display default value)




Change Properties
Action