From 5fb50c35e575a31ac1aa9d36e4573d7e01860ed8 Mon Sep 17 00:00:00 2001 From: Lars Wiegman Date: Fri, 13 Jan 2012 18:54:55 +0100 Subject: [PATCH 1/3] Autocompleet fields if they have a default value set in the model. Otherwise, what's the point of having a default value in the model if they aren't used during form validation. A better place for this would be in Django forms or db module. --- djangorestframework/resources.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index 68b285b91..493446305 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -104,6 +104,25 @@ class FormResource(Resource): to be populated when an empty dict is supplied in `data` """ + # Autocompleet fields if they have a default value set in the model. + # Otherwise, what's the point of having a default value in the model if + # they aren't used during form validation. + # A better place for this would be in Django forms or db module. + if hasattr(self.model, '_meta'): + data_mutable = data.copy() + for field in self.model._meta.fields: + # Exclude the id and pk + exclude = self.exclude + try: + # Excludes set in ModelForm + exclude += self.form.Meta.exclude + except AttributeError: + pass + if field.has_default() and field.name not in data and field.name not in exclude: + default = field.get_default() + data_mutable.update({field.name:default}) + data = data_mutable + # We'd like nice error messages even if no content is supplied. # Typically if an empty dict is given to a form Django will # return .is_valid() == False, but .errors == {} From aac479c48b3e1201114481bdd5185cf12827dca4 Mon Sep 17 00:00:00 2001 From: Lars Wiegman Date: Sat, 14 Jan 2012 14:26:28 +0100 Subject: [PATCH 2/3] Added an exception to only auto-complete field when whe're creating new objects, during POST methods. --- djangorestframework/resources.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index 493446305..9a8e02186 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -108,17 +108,17 @@ class FormResource(Resource): # Otherwise, what's the point of having a default value in the model if # they aren't used during form validation. # A better place for this would be in Django forms or db module. - if hasattr(self.model, '_meta'): + method = self.view._method if hasattr(self.view, '_method') else None + if 'POST' == method and hasattr(self.model, '_meta'): data_mutable = data.copy() for field in self.model._meta.fields: - # Exclude the id and pk - exclude = self.exclude + # Gather excludes like id and pk from self and Meta + exclude = self.exclude + tuple(data.keys()) try: - # Excludes set in ModelForm exclude += self.form.Meta.exclude except AttributeError: pass - if field.has_default() and field.name not in data and field.name not in exclude: + if field.name not in exclude and field.has_default(): default = field.get_default() data_mutable.update({field.name:default}) data = data_mutable From 6459b929f5b3ed565b50799686731d67e74292b3 Mon Sep 17 00:00:00 2001 From: Lars Wiegman Date: Sat, 14 Jan 2012 15:04:08 +0100 Subject: [PATCH 3/3] Improved comments to better explain the need for auto-completion --- djangorestframework/resources.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index 9a8e02186..8900d3936 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -104,10 +104,11 @@ class FormResource(Resource): to be populated when an empty dict is supplied in `data` """ - # Autocompleet fields if they have a default value set in the model. - # Otherwise, what's the point of having a default value in the model if - # they aren't used during form validation. - # A better place for this would be in Django forms or db module. + # Auto-complete fields with the default value on a POST request. + # + # While Django web forms have auto-populate, POST requests in REST + # should have auto-complete. Otherwise, what's the point + # of having a default value. method = self.view._method if hasattr(self.view, '_method') else None if 'POST' == method and hasattr(self.model, '_meta'): data_mutable = data.copy()