From 7442b933fd68bb81b3f04265b8d1e6a6613fa92f Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 14 Oct 2017 17:55:29 -0700 Subject: [PATCH] Add support for setting default field values on new forms i.e. those using Colander schema --- tailbone/forms2/core.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tailbone/forms2/core.py b/tailbone/forms2/core.py index c1cf9782..3dcd46e3 100644 --- a/tailbone/forms2/core.py +++ b/tailbone/forms2/core.py @@ -177,8 +177,8 @@ class Form(object): """ def __init__(self, fields=None, schema=None, request=None, readonly=False, readonly_fields=[], - model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={}, widgets={}, - action_url=None, cancel_url=None): + model_instance=None, model_class=None, nodes={}, enums={}, labels={}, renderers={}, + widgets={}, defaults={}, action_url=None, cancel_url=None): self.fields = list(fields) if fields is not None else None self.schema = schema @@ -196,6 +196,7 @@ class Form(object): self.labels = labels or {} self.renderers = renderers or {} self.widgets = widgets or {} + self.defaults = defaults or {} self.action_url = action_url self.cancel_url = cancel_url @@ -265,6 +266,11 @@ class Form(object): if key in schema: schema[key].widget = widget + # apply any default values + for key, default in self.defaults.items(): + if key in schema: + schema[key].default = default + self.schema = schema return self.schema @@ -330,7 +336,7 @@ class Form(object): """ Set the default value for a given field. """ - raise NotImplementedError # TODO + self.defaults[key] = value def render(self, template=None, **kwargs): if not template: