aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2012-09-08 22:13:48 +0100
committerTom Christie2012-09-08 22:13:48 +0100
commit27a51094851ff8fe93ff4f604f0f1daee44c770a (patch)
tree04b7e4171ccb32a0dfab5474f83b3038917aae42
parentb5e07abc15f0937263e183b7800f30b558e37a1b (diff)
parenta33c21764a95d1d7ceb632e09b7a0bdd7010ad2f (diff)
downloaddjango-rest-framework-27a51094851ff8fe93ff4f604f0f1daee44c770a.tar.bz2
Merge branch 'restframework2' of https://github.com/tomchristie/django-rest-framework into restframework2
-rw-r--r--djangorestframework/mixins.py2
-rw-r--r--djangorestframework/renderers.py53
-rw-r--r--djangorestframework/templates/djangorestframework/base.html5
3 files changed, 28 insertions, 32 deletions
diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py
index 6ab7ab6e..1f06dd34 100644
--- a/djangorestframework/mixins.py
+++ b/djangorestframework/mixins.py
@@ -22,7 +22,7 @@ class CreateModelMixin(object):
self.object = serializer.object
self.object.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
- return Response(serializer.error_data, status=status.HTTP_400_BAD_REQUEST)
+ return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class ListModelMixin(object):
diff --git a/djangorestframework/renderers.py b/djangorestframework/renderers.py
index 4f8225b1..45cdbbbb 100644
--- a/djangorestframework/renderers.py
+++ b/djangorestframework/renderers.py
@@ -16,6 +16,7 @@ from djangorestframework.utils import encoders
from djangorestframework.utils.breadcrumbs import get_breadcrumbs
from djangorestframework.utils.mediatypes import get_media_type_params, add_media_type_param, media_type_matches
from djangorestframework import VERSION
+from djangorestframework.fields import FloatField, IntegerField, DateTimeField, DateField, EmailField, CharField, BooleanField
import string
@@ -233,33 +234,31 @@ class DocumentingTemplateRenderer(BaseRenderer):
In the absence on of the Resource having an associated form then
provide a form that can be used to submit arbitrary content.
"""
-
- # Get the form instance if we have one bound to the input
- form_instance = None
- if method == getattr(view, 'method', view.request.method).lower():
- form_instance = getattr(view, 'bound_form_instance', None)
-
- if not form_instance and hasattr(view, 'get_bound_form'):
- # Otherwise if we have a response that is valid against the form then use that
- if view.response.has_content_body:
- try:
- form_instance = view.get_bound_form(view.response.cleaned_content, method=method)
- if form_instance and not form_instance.is_valid():
- form_instance = None
- except Exception:
- form_instance = None
-
- # If we still don't have a form instance then try to get an unbound form
- if not form_instance:
- try:
- form_instance = view.get_bound_form(method=method)
- except Exception:
- pass
-
- # If we still don't have a form instance then try to get an unbound form which can tunnel arbitrary content types
- if not form_instance:
- form_instance = self._get_generic_content_form(view)
-
+ if not hasattr(self.view, 'get_serializer'): # No serializer, no form.
+ return
+ # We need to map our Fields to Django's Fields.
+ field_mapping = dict([
+ [FloatField.__name__, forms.FloatField],
+ [IntegerField.__name__, forms.IntegerField],
+ [DateTimeField.__name__, forms.DateTimeField],
+ [DateField.__name__, forms.DateField],
+ [EmailField.__name__, forms.EmailField],
+ [CharField.__name__, forms.CharField],
+ [BooleanField.__name__, forms.BooleanField]
+ ])
+
+ # Creating an on the fly form see: http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
+ fields = {}
+ object, data = None, None
+ if hasattr(self.view, 'object'):
+ object = self.view.object
+ serializer = self.view.get_serializer(instance=object)
+ for k, v in serializer.fields.items():
+ fields[k] = field_mapping[v.__class__.__name__]()
+ OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
+ if object and not self.view.request.method == 'DELETE': # Don't fill in the form when the object is deleted
+ data = serializer.data
+ form_instance = OnTheFlyForm(data)
return form_instance
def _get_generic_content_form(self, view):
diff --git a/djangorestframework/templates/djangorestframework/base.html b/djangorestframework/templates/djangorestframework/base.html
index d33c5393..97e81bbe 100644
--- a/djangorestframework/templates/djangorestframework/base.html
+++ b/djangorestframework/templates/djangorestframework/base.html
@@ -78,8 +78,6 @@
</form>
{% endif %}
- {% comment %}
- DROP POST/PUT/DELETE until the forms are working with REST framework 2
{# Only display the POST/PUT/DELETE forms if method tunneling via POST forms is enabled and the user has permissions on this view. #}
{% if response.status_code != 403 %}
@@ -90,7 +88,7 @@
<h2>POST {{ name }}</h2>
{% csrf_token %}
{{ post_form.non_field_errors }}
- {% for field in post_form %}
+ {% for field in post_form %}
<div class='form-row'>
{{ field.label_tag }}
{{ field }}
@@ -141,7 +139,6 @@
{% endif %}
{% endif %}
- {% endcomment %}
</div>
<!-- END content-main -->