Opened 5 years ago

Last modified 5 years ago

#15725 new New feature

No easy way to get default model values into a ModelForm for validation

Reported by: mark0978 Owned by: nobody
Component: Forms Version: 1.2
Severity: Normal Keywords:
Cc: tomchristie Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have a Django modelForm that excludes some model fields so that when it is used on a web page, those fields are available. I'd like to use the same modelForm object on an API.

When I use the form on a webpage, the rendering of the form takes into account the default values of the model Field, the form is rendered and the submitted "final" form contains those values. Similarly I can do the same thing with an initial dict passed to the form during creation.

Problem is, when you expose a modelForm via an API there is no initial rendering of the default values to cause them to show up in the final submitted form. What this results in is is_valid returning false because the form "requires" fields that would have been filled in with their default values via the initial rendering of the form.

I've covered it all here with a possible fix:

Change History (6)

comment:1 Changed 5 years ago by lukeplant

  • Type set to New feature

comment:2 Changed 5 years ago by lukeplant

  • Severity set to Normal

comment:3 Changed 5 years ago by tomchristie

  • Cc tomchristie added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:4 Changed 5 years ago by carljm

  • Component changed from Uncategorized to Forms
  • Triage Stage changed from Unreviewed to Accepted

It seems reasonable to me that default values from model fields, if present, would be used as default values for missing keys in data for a bound form, before it is validated.

comment:5 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

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