From e80d3d1bdfb071a288340555cb6fe8a9bce3772d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sun, 17 Mar 2013 19:51:04 +0000 Subject: [PATCH 1/4] Fix text regarding serializer error messages. Closes #734. --- docs/api-guide/serializers.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index de2cf7d8f..e7e1670b2 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -91,9 +91,10 @@ To serialize a queryset instead of an object instance, you should pass the `many ## Validation -When deserializing data, you always need to call `is_valid()` before attempting to access the deserialized object. If any validation errors occur, the `.errors` and `.non_field_errors` properties will contain the resulting error messages. +When deserializing data, you always need to call `is_valid()` before attempting to access the deserialized object. If any validation errors occur, the `.errors` property will contain a dictionary representing the resulting error messages. +Each key in the dictionary will be the field name, and the values will be lists of strings of any error messages corresponding to that field. The `non_field_errors` key may also be present, and will list any general validation errors. -When deserialising a list of items, errors will be returned as a list of tuples. The first item in an error tuple will be the index of the item with the error in the original data; The second item in the tuple will be a dict with the individual errors for that item. +When deserializing a list of items, errors will be returned as a list of dictionaries representing each of the deserialized items. ### Field-level validation From ef0caf64d326bacdf367e002e5537b8c0d444d34 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sun, 17 Mar 2013 19:59:13 +0000 Subject: [PATCH 2/4] Extra note on method --- docs/api-guide/serializers.md | 8 ++++++++ docs/tutorial/1-serialization.md | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index e7e1670b2..e8d3c1b53 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -25,6 +25,7 @@ Let's start by creating a simple object we can use for example purposes: comment = Comment(email='leila@example.com', content='foo bar') We'll declare a serializer that we can use to serialize and deserialize `Comment` objects. + Declaring a serializer looks very similar to declaring a form: class CommentSerializer(serializers.Serializer): @@ -33,6 +34,13 @@ Declaring a serializer looks very similar to declaring a form: created = serializers.DateTimeField() def restore_object(self, attrs, instance=None): + """ + Given a dictionary of deserialized field values, either update + an existing model instance, or create a new model instance. + + Note that if we don't define this method, then deserializing + data will simply return a dictionary of items. + """ if instance is not None: instance.title = attrs['title'] instance.content = attrs['content'] diff --git a/docs/tutorial/1-serialization.md b/docs/tutorial/1-serialization.md index 6709f7511..205ee7e02 100644 --- a/docs/tutorial/1-serialization.md +++ b/docs/tutorial/1-serialization.md @@ -126,7 +126,11 @@ The first thing we need to get started on our Web API is provide a way of serial def restore_object(self, attrs, instance=None): """ - Create or update a new snippet instance. + Create or update a new snippet instance, given a dictionary + of deserialized field values. + + Note that if we don't define this method, then deserializing + data will simply return a dictionary of items. """ if instance: # Update existing instance From 034c4ce4081dd6d15ea47fb8318754321a3faf0c Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sun, 17 Mar 2013 20:06:38 +0000 Subject: [PATCH 3/4] Fix serializer restore_object example for partial updates --- docs/api-guide/serializers.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index e8d3c1b53..42edf9af1 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -42,9 +42,9 @@ Declaring a serializer looks very similar to declaring a form: data will simply return a dictionary of items. """ if instance is not None: - instance.title = attrs['title'] - instance.content = attrs['content'] - instance.created = attrs['created'] + instance.title = attrs.get('title', instance.title) + instance.content = attrs.get('content', instance.content) + instance.created = attrs.get('created', instance.created) return instance return Comment(**attrs) From 09e4ee7ae332326e77b23bac1539d31e582419e9 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 18 Mar 2013 21:11:40 +0000 Subject: [PATCH 4/4] Remove dumbass unneeded test --- rest_framework/tests/status.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 rest_framework/tests/status.py diff --git a/rest_framework/tests/status.py b/rest_framework/tests/status.py deleted file mode 100644 index e1644a6b4..000000000 --- a/rest_framework/tests/status.py +++ /dev/null @@ -1,13 +0,0 @@ -"""Tests for the status module""" -from __future__ import unicode_literals -from django.test import TestCase -from rest_framework import status - - -class TestStatus(TestCase): - """Simple sanity test to check the status module""" - - def test_status(self): - """Ensure the status module is present and correct.""" - self.assertEqual(200, status.HTTP_200_OK) - self.assertEqual(404, status.HTTP_404_NOT_FOUND)