Commit c37bd40d by Tom Christie
parents 70b07981 2bea7647
...@@ -82,7 +82,7 @@ Note that the exception handler will only be called for responses generated by r ...@@ -82,7 +82,7 @@ Note that the exception handler will only be called for responses generated by r
## APIException ## APIException
**Signature:** `APIException(detail=None)` **Signature:** `APIException()`
The **base class** for all exceptions raised inside REST framework. The **base class** for all exceptions raised inside REST framework.
......
...@@ -54,7 +54,7 @@ Would serialize to the following representation. ...@@ -54,7 +54,7 @@ Would serialize to the following representation.
{ {
'album_name': 'Things We Lost In The Fire', 'album_name': 'Things We Lost In The Fire',
'artist': 'Low' 'artist': 'Low',
'tracks': [ 'tracks': [
'1: Sunflower', '1: Sunflower',
'2: Whitetail', '2: Whitetail',
...@@ -86,7 +86,7 @@ Would serialize to a representation like this: ...@@ -86,7 +86,7 @@ Would serialize to a representation like this:
{ {
'album_name': 'The Roots', 'album_name': 'The Roots',
'artist': 'Undun' 'artist': 'Undun',
'tracks': [ 'tracks': [
89, 89,
90, 90,
...@@ -121,7 +121,7 @@ Would serialize to a representation like this: ...@@ -121,7 +121,7 @@ Would serialize to a representation like this:
{ {
'album_name': 'Graceland', 'album_name': 'Graceland',
'artist': 'Paul Simon' 'artist': 'Paul Simon',
'tracks': [ 'tracks': [
'http://www.example.com/api/tracks/45/', 'http://www.example.com/api/tracks/45/',
'http://www.example.com/api/tracks/46/', 'http://www.example.com/api/tracks/46/',
...@@ -159,7 +159,7 @@ Would serialize to a representation like this: ...@@ -159,7 +159,7 @@ Would serialize to a representation like this:
{ {
'album_name': 'Dear John', 'album_name': 'Dear John',
'artist': 'Loney Dear' 'artist': 'Loney Dear',
'tracks': [ 'tracks': [
'Airport Surroundings', 'Airport Surroundings',
'Everything Turns to You', 'Everything Turns to You',
...@@ -194,7 +194,7 @@ Would serialize to a representation like this: ...@@ -194,7 +194,7 @@ Would serialize to a representation like this:
{ {
'album_name': 'The Eraser', 'album_name': 'The Eraser',
'artist': 'Thom Yorke' 'artist': 'Thom Yorke',
'track_listing': 'http://www.example.com/api/track_list/12/', 'track_listing': 'http://www.example.com/api/track_list/12/',
} }
...@@ -234,7 +234,7 @@ Would serialize to a nested representation like this: ...@@ -234,7 +234,7 @@ Would serialize to a nested representation like this:
{ {
'album_name': 'The Grey Album', 'album_name': 'The Grey Album',
'artist': 'Danger Mouse' 'artist': 'Danger Mouse',
'tracks': [ 'tracks': [
{'order': 1, 'title': 'Public Service Announcement'}, {'order': 1, 'title': 'Public Service Announcement'},
{'order': 2, 'title': 'What More Can I Say'}, {'order': 2, 'title': 'What More Can I Say'},
...@@ -271,7 +271,7 @@ This custom field would then serialize to the following representation. ...@@ -271,7 +271,7 @@ This custom field would then serialize to the following representation.
{ {
'album_name': 'Sometimes I Wish We Were an Eagle', 'album_name': 'Sometimes I Wish We Were an Eagle',
'artist': 'Bill Callahan' 'artist': 'Bill Callahan',
'tracks': [ 'tracks': [
'Track 1: Jim Cain (04:39)', 'Track 1: Jim Cain (04:39)',
'Track 2: Eid Ma Clack Shaw (04:19)', 'Track 2: Eid Ma Clack Shaw (04:19)',
......
...@@ -171,6 +171,8 @@ The following people have helped make REST framework great. ...@@ -171,6 +171,8 @@ The following people have helped make REST framework great.
* Tai Lee - [mrmachine] * Tai Lee - [mrmachine]
* Markus Kaiserswerth - [mkai] * Markus Kaiserswerth - [mkai]
* Henry Clifford - [hcliff] * Henry Clifford - [hcliff]
* Thomas Badaud - [badale]
* Colin Huang - [tamakisquare]
Many thanks to everyone who's contributed to the project. Many thanks to everyone who's contributed to the project.
...@@ -378,3 +380,5 @@ You can also contact [@_tomchristie][twitter] directly on twitter. ...@@ -378,3 +380,5 @@ You can also contact [@_tomchristie][twitter] directly on twitter.
[mrmachine]: https://github.com/mrmachine [mrmachine]: https://github.com/mrmachine
[mkai]: https://github.com/mkai [mkai]: https://github.com/mkai
[hcliff]: https://github.com/hcliff [hcliff]: https://github.com/hcliff
[badale]: https://github.com/badale
[tamakisquare]: https://github.com/tamakisquare
...@@ -35,7 +35,7 @@ The wrappers also provide behaviour such as returning `405 Method Not Allowed` r ...@@ -35,7 +35,7 @@ The wrappers also provide behaviour such as returning `405 Method Not Allowed` r
Okay, let's go ahead and start using these new components to write a few views. Okay, let's go ahead and start using these new components to write a few views.
We don't need our `JSONResponse` class anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly. We don't need our `JSONResponse` class in `views.py` anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly.
from rest_framework import status from rest_framework import status
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
...@@ -64,7 +64,7 @@ We don't need our `JSONResponse` class anymore, so go ahead and delete that. On ...@@ -64,7 +64,7 @@ We don't need our `JSONResponse` class anymore, so go ahead and delete that. On
Our instance view is an improvement over the previous example. It's a little more concise, and the code now feels very similar to if we were working with the Forms API. We're also using named status codes, which makes the response meanings more obvious. Our instance view is an improvement over the previous example. It's a little more concise, and the code now feels very similar to if we were working with the Forms API. We're also using named status codes, which makes the response meanings more obvious.
Here is the view for an individual snippet. Here is the view for an individual snippet, in the `views.py` module.
@api_view(['GET', 'PUT', 'DELETE']) @api_view(['GET', 'PUT', 'DELETE'])
def snippet_detail(request, pk): def snippet_detail(request, pk):
......
...@@ -4,7 +4,7 @@ We can also write our API views using class based views, rather than function ba ...@@ -4,7 +4,7 @@ We can also write our API views using class based views, rather than function ba
## Rewriting our API using class based views ## Rewriting our API using class based views
We'll start by rewriting the root view as a class based view. All this involves is a little bit of refactoring. We'll start by rewriting the root view as a class based view. All this involves is a little bit of refactoring of `views.py`.
from snippets.models import Snippet from snippets.models import Snippet
from snippets.serializers import SnippetSerializer from snippets.serializers import SnippetSerializer
...@@ -30,7 +30,7 @@ We'll start by rewriting the root view as a class based view. All this involves ...@@ -30,7 +30,7 @@ We'll start by rewriting the root view as a class based view. All this involves
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view. So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `views.py`.
class SnippetDetail(APIView): class SnippetDetail(APIView):
""" """
...@@ -62,7 +62,7 @@ So far, so good. It looks pretty similar to the previous case, but we've got be ...@@ -62,7 +62,7 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
That's looking good. Again, it's still pretty similar to the function based view right now. That's looking good. Again, it's still pretty similar to the function based view right now.
We'll also need to refactor our URLconf slightly now we're using class based views. We'll also need to refactor our `urls.py` slightly now we're using class based views.
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns from rest_framework.urlpatterns import format_suffix_patterns
...@@ -83,7 +83,7 @@ One of the big wins of using class based views is that it allows us to easily co ...@@ -83,7 +83,7 @@ One of the big wins of using class based views is that it allows us to easily co
The create/retrieve/update/delete operations that we've been using so far are going to be pretty similar for any model-backed API views we create. Those bits of common behaviour are implemented in REST framework's mixin classes. The create/retrieve/update/delete operations that we've been using so far are going to be pretty similar for any model-backed API views we create. Those bits of common behaviour are implemented in REST framework's mixin classes.
Let's take a look at how we can compose our views by using the mixin classes. Let's take a look at how we can compose the views by using the mixin classes. Here's our `views.py` module again.
from snippets.models import Snippet from snippets.models import Snippet
from snippets.serializers import SnippetSerializer from snippets.serializers import SnippetSerializer
...@@ -126,7 +126,7 @@ Pretty similar. Again we're using the `GenericAPIView` class to provide the cor ...@@ -126,7 +126,7 @@ Pretty similar. Again we're using the `GenericAPIView` class to provide the cor
## Using generic class based views ## Using generic class based views
Using the mixin classes we've rewritten the views to use slightly less code than before, but we can go one step further. REST framework provides a set of already mixed-in generic views that we can use. Using the mixin classes we've rewritten the views to use slightly less code than before, but we can go one step further. REST framework provides a set of already mixed-in generic views that we can use to trim down our `views.py` module even more.
from snippets.models import Snippet from snippets.models import Snippet
from snippets.serializers import SnippetSerializer from snippets.serializers import SnippetSerializer
......
...@@ -12,7 +12,7 @@ Currently our API doesn't have any restrictions on who can edit or delete code s ...@@ -12,7 +12,7 @@ Currently our API doesn't have any restrictions on who can edit or delete code s
We're going to make a couple of changes to our `Snippet` model class. We're going to make a couple of changes to our `Snippet` model class.
First, let's add a couple of fields. One of those fields will be used to represent the user who created the code snippet. The other field will be used to store the highlighted HTML representation of the code. First, let's add a couple of fields. One of those fields will be used to represent the user who created the code snippet. The other field will be used to store the highlighted HTML representation of the code.
Add the following two fields to the model. Add the following two fields to the `Snippet` model in `models.py`.
owner = models.ForeignKey('auth.User', related_name='snippets') owner = models.ForeignKey('auth.User', related_name='snippets')
highlighted = models.TextField() highlighted = models.TextField()
...@@ -52,7 +52,7 @@ You might also want to create a few different users, to use for testing the API. ...@@ -52,7 +52,7 @@ You might also want to create a few different users, to use for testing the API.
## Adding endpoints for our User models ## Adding endpoints for our User models
Now that we've got some users to work with, we'd better add representations of those users to our API. Creating a new serializer is easy: Now that we've got some users to work with, we'd better add representations of those users to our API. Creating a new serializer is easy. In `serializers.py` add:
from django.contrib.auth.models import User from django.contrib.auth.models import User
...@@ -65,7 +65,7 @@ Now that we've got some users to work with, we'd better add representations of t ...@@ -65,7 +65,7 @@ Now that we've got some users to work with, we'd better add representations of t
Because `'snippets'` is a *reverse* relationship on the User model, it will not be included by default when using the `ModelSerializer` class, so we needed to add an explicit field for it. Because `'snippets'` is a *reverse* relationship on the User model, it will not be included by default when using the `ModelSerializer` class, so we needed to add an explicit field for it.
We'll also add a couple of views. We'd like to just use read-only views for the user representations, so we'll use the `ListAPIView` and `RetrieveAPIView` generic class based views. We'll also add a couple of views to `views.py`. We'd like to just use read-only views for the user representations, so we'll use the `ListAPIView` and `RetrieveAPIView` generic class based views.
class UserList(generics.ListAPIView): class UserList(generics.ListAPIView):
queryset = User.objects.all() queryset = User.objects.all()
...@@ -75,8 +75,12 @@ We'll also add a couple of views. We'd like to just use read-only views for the ...@@ -75,8 +75,12 @@ We'll also add a couple of views. We'd like to just use read-only views for the
class UserDetail(generics.RetrieveAPIView): class UserDetail(generics.RetrieveAPIView):
queryset = User.objects.all() queryset = User.objects.all()
serializer_class = UserSerializer serializer_class = UserSerializer
Make sure to also import the `UserSerializer` class
Finally we need to add those views into the API, by referencing them from the URL conf. from snippets.serializers import UserSerializer
Finally we need to add those views into the API, by referencing them from the URL conf. Add the following to the patterns in `urls.py`.
url(r'^users/$', views.UserList.as_view()), url(r'^users/$', views.UserList.as_view()),
url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()), url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
...@@ -94,7 +98,7 @@ On **both** the `SnippetList` and `SnippetDetail` view classes, add the followin ...@@ -94,7 +98,7 @@ On **both** the `SnippetList` and `SnippetDetail` view classes, add the followin
## Updating our serializer ## Updating our serializer
Now that snippets are associated with the user that created them, let's update our `SnippetSerializer` to reflect that. Add the following field to the serializer definition: Now that snippets are associated with the user that created them, let's update our `SnippetSerializer` to reflect that. Add the following field to the serializer definition in `serializers.py`:
owner = serializers.Field(source='owner.username') owner = serializers.Field(source='owner.username')
......
...@@ -262,10 +262,13 @@ class BaseSerializer(WritableField): ...@@ -262,10 +262,13 @@ class BaseSerializer(WritableField):
for field_name, field in self.fields.items(): for field_name, field in self.fields.items():
if field_name in self._errors: if field_name in self._errors:
continue continue
source = field.source or field_name
if self.partial and source not in attrs:
continue
try: try:
validate_method = getattr(self, 'validate_%s' % field_name, None) validate_method = getattr(self, 'validate_%s' % field_name, None)
if validate_method: if validate_method:
source = field.source or field_name
attrs = validate_method(attrs, source) attrs = validate_method(attrs, source)
except ValidationError as err: except ValidationError as err:
self._errors[field_name] = self._errors.get(field_name, []) + list(err.messages) self._errors[field_name] = self._errors.get(field_name, []) + list(err.messages)
...@@ -403,7 +406,7 @@ class BaseSerializer(WritableField): ...@@ -403,7 +406,7 @@ class BaseSerializer(WritableField):
return return
# Set the serializer object if it exists # Set the serializer object if it exists
obj = getattr(self.parent.object, field_name) if self.parent.object else None obj = get_component(self.parent.object, self.source or field_name) if self.parent.object else None
obj = obj.all() if is_simple_callable(getattr(obj, 'all', None)) else obj obj = obj.all() if is_simple_callable(getattr(obj, 'all', None)) else obj
if self.source == '*': if self.source == '*':
...@@ -912,7 +915,7 @@ class ModelSerializer(Serializer): ...@@ -912,7 +915,7 @@ class ModelSerializer(Serializer):
def save_object(self, obj, **kwargs): def save_object(self, obj, **kwargs):
""" """
Save the deserialized object and return it. Save the deserialized object.
""" """
if getattr(obj, '_nested_forward_relations', None): if getattr(obj, '_nested_forward_relations', None):
# Nested relationships need to be saved before we can save the # Nested relationships need to be saved before we can save the
......
...@@ -110,7 +110,9 @@ ...@@ -110,7 +110,9 @@
<div class="content-main"> <div class="content-main">
<div class="page-header"><h1>{{ name }}</h1></div> <div class="page-header"><h1>{{ name }}</h1></div>
{% block description %}
{{ description }} {{ description }}
{% endblock %}
<div class="request-info" style="clear: both" > <div class="request-info" style="clear: both" >
<pre class="prettyprint"><b>{{ request.method }}</b> {{ request.get_full_path }}</pre> <pre class="prettyprint"><b>{{ request.method }}</b> {{ request.get_full_path }}</pre>
</div> </div>
......
...@@ -328,7 +328,7 @@ if yaml: ...@@ -328,7 +328,7 @@ if yaml:
class YAMLRendererTests(TestCase): class YAMLRendererTests(TestCase):
""" """
Tests specific to the JSON Renderer Tests specific to the YAML Renderer
""" """
def test_render(self): def test_render(self):
...@@ -354,6 +354,17 @@ if yaml: ...@@ -354,6 +354,17 @@ if yaml:
data = parser.parse(StringIO(content)) data = parser.parse(StringIO(content))
self.assertEqual(obj, data) self.assertEqual(obj, data)
def test_render_decimal(self):
"""
Test YAML decimal rendering.
"""
renderer = YAMLRenderer()
content = renderer.render({'field': Decimal('111.2')}, 'application/yaml')
self.assertYAMLContains(content, "field: '111.2'")
def assertYAMLContains(self, content, string):
self.assertTrue(string in content, '%r not in %r' % (string, content))
class XMLRendererTestCase(TestCase): class XMLRendererTestCase(TestCase):
""" """
......
...@@ -511,6 +511,33 @@ class CustomValidationTests(TestCase): ...@@ -511,6 +511,33 @@ class CustomValidationTests(TestCase):
self.assertFalse(serializer.is_valid()) self.assertFalse(serializer.is_valid())
self.assertEqual(serializer.errors, {'email': ['Enter a valid email address.']}) self.assertEqual(serializer.errors, {'email': ['Enter a valid email address.']})
def test_partial_update(self):
"""
Make sure that validate_email isn't called when partial=True and email
isn't found in data.
"""
initial_data = {
'email': 'tom@example.com',
'content': 'A test comment',
'created': datetime.datetime(2012, 1, 1)
}
serializer = self.CommentSerializerWithFieldValidator(data=initial_data)
self.assertEqual(serializer.is_valid(), True)
instance = serializer.object
new_content = 'An *updated* test comment'
partial_data = {
'content': new_content
}
serializer = self.CommentSerializerWithFieldValidator(instance=instance,
data=partial_data,
partial=True)
self.assertEqual(serializer.is_valid(), True)
instance = serializer.object
self.assertEqual(instance.content, new_content)
class PositiveIntegerAsChoiceTests(TestCase): class PositiveIntegerAsChoiceTests(TestCase):
def test_positive_integer_in_json_is_correctly_parsed(self): def test_positive_integer_in_json_is_correctly_parsed(self):
......
...@@ -244,3 +244,70 @@ class WritableNestedSerializerObjectTests(TestCase): ...@@ -244,3 +244,70 @@ class WritableNestedSerializerObjectTests(TestCase):
serializer = self.AlbumSerializer(data=data, many=True) serializer = self.AlbumSerializer(data=data, many=True)
self.assertEqual(serializer.is_valid(), True) self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected_object) self.assertEqual(serializer.object, expected_object)
class ForeignKeyNestedSerializerUpdateTests(TestCase):
def setUp(self):
class Artist(object):
def __init__(self, name):
self.name = name
def __eq__(self, other):
return self.name == other.name
class Album(object):
def __init__(self, name, artist):
self.name, self.artist = name, artist
def __eq__(self, other):
return self.name == other.name and self.artist == other.artist
class ArtistSerializer(serializers.Serializer):
name = serializers.CharField()
def restore_object(self, attrs, instance=None):
if instance:
instance.name = attrs['name']
else:
instance = Artist(attrs['name'])
return instance
class AlbumSerializer(serializers.Serializer):
name = serializers.CharField()
by = ArtistSerializer(source='artist')
def restore_object(self, attrs, instance=None):
if instance:
instance.name = attrs['name']
instance.artist = attrs['artist']
else:
instance = Album(attrs['name'], attrs['artist'])
return instance
self.Artist = Artist
self.Album = Album
self.AlbumSerializer = AlbumSerializer
def test_create_via_foreign_key_with_source(self):
"""
Check that we can both *create* and *update* into objects across
ForeignKeys that have a `source` specified.
Regression test for #1170
"""
data = {
'name': 'Discovery',
'by': {'name': 'Daft Punk'},
}
expected = self.Album(artist=self.Artist('Daft Punk'), name='Discovery')
# create
serializer = self.AlbumSerializer(data=data)
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
# update
original = self.Album(artist=self.Artist('The Bats'), name='Free All the Monsters')
serializer = self.AlbumSerializer(instance=original, data=data)
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
...@@ -89,6 +89,9 @@ else: ...@@ -89,6 +89,9 @@ else:
node.flow_style = best_style node.flow_style = best_style
return node return node
SafeDumper.add_representer(decimal.Decimal,
SafeDumper.represent_decimal)
SafeDumper.add_representer(SortedDict, SafeDumper.add_representer(SortedDict,
yaml.representer.SafeRepresenter.represent_dict) yaml.representer.SafeRepresenter.represent_dict)
SafeDumper.add_representer(DictWithMetadata, SafeDumper.add_representer(DictWithMetadata,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment