Skip to content

Commit c37bd40

Browse files
committed
2 parents 70b0798 + 2bea764 commit c37bd40

12 files changed

+145
-24
lines changed

docs/api-guide/exceptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Note that the exception handler will only be called for responses generated by r
8282

8383
## APIException
8484

85-
**Signature:** `APIException(detail=None)`
85+
**Signature:** `APIException()`
8686

8787
The **base class** for all exceptions raised inside REST framework.
8888

docs/api-guide/relations.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Would serialize to the following representation.
5454

5555
{
5656
'album_name': 'Things We Lost In The Fire',
57-
'artist': 'Low'
57+
'artist': 'Low',
5858
'tracks': [
5959
'1: Sunflower',
6060
'2: Whitetail',
@@ -86,7 +86,7 @@ Would serialize to a representation like this:
8686

8787
{
8888
'album_name': 'The Roots',
89-
'artist': 'Undun'
89+
'artist': 'Undun',
9090
'tracks': [
9191
89,
9292
90,
@@ -121,7 +121,7 @@ Would serialize to a representation like this:
121121

122122
{
123123
'album_name': 'Graceland',
124-
'artist': 'Paul Simon'
124+
'artist': 'Paul Simon',
125125
'tracks': [
126126
'http://www.example.com/api/tracks/45/',
127127
'http://www.example.com/api/tracks/46/',
@@ -159,7 +159,7 @@ Would serialize to a representation like this:
159159

160160
{
161161
'album_name': 'Dear John',
162-
'artist': 'Loney Dear'
162+
'artist': 'Loney Dear',
163163
'tracks': [
164164
'Airport Surroundings',
165165
'Everything Turns to You',
@@ -194,7 +194,7 @@ Would serialize to a representation like this:
194194

195195
{
196196
'album_name': 'The Eraser',
197-
'artist': 'Thom Yorke'
197+
'artist': 'Thom Yorke',
198198
'track_listing': 'http://www.example.com/api/track_list/12/',
199199
}
200200

@@ -234,7 +234,7 @@ Would serialize to a nested representation like this:
234234

235235
{
236236
'album_name': 'The Grey Album',
237-
'artist': 'Danger Mouse'
237+
'artist': 'Danger Mouse',
238238
'tracks': [
239239
{'order': 1, 'title': 'Public Service Announcement'},
240240
{'order': 2, 'title': 'What More Can I Say'},
@@ -271,7 +271,7 @@ This custom field would then serialize to the following representation.
271271

272272
{
273273
'album_name': 'Sometimes I Wish We Were an Eagle',
274-
'artist': 'Bill Callahan'
274+
'artist': 'Bill Callahan',
275275
'tracks': [
276276
'Track 1: Jim Cain (04:39)',
277277
'Track 2: Eid Ma Clack Shaw (04:19)',

docs/topics/credits.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ The following people have helped make REST framework great.
171171
* Tai Lee - [mrmachine]
172172
* Markus Kaiserswerth - [mkai]
173173
* Henry Clifford - [hcliff]
174+
* Thomas Badaud - [badale]
175+
* Colin Huang - [tamakisquare]
174176

175177
Many thanks to everyone who's contributed to the project.
176178

@@ -378,3 +380,5 @@ You can also contact [@_tomchristie][twitter] directly on twitter.
378380
[mrmachine]: https://github.com/mrmachine
379381
[mkai]: https://github.com/mkai
380382
[hcliff]: https://github.com/hcliff
383+
[badale]: https://github.com/badale
384+
[tamakisquare]: https://github.com/tamakisquare

docs/tutorial/2-requests-and-responses.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The wrappers also provide behaviour such as returning `405 Method Not Allowed` r
3535

3636
Okay, let's go ahead and start using these new components to write a few views.
3737

38-
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.
38+
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.
3939

4040
from rest_framework import status
4141
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
6464

6565
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.
6666

67-
Here is the view for an individual snippet.
67+
Here is the view for an individual snippet, in the `views.py` module.
6868

6969
@api_view(['GET', 'PUT', 'DELETE'])
7070
def snippet_detail(request, pk):

docs/tutorial/3-class-based-views.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ We can also write our API views using class based views, rather than function ba
44

55
## Rewriting our API using class based views
66

7-
We'll start by rewriting the root view as a class based view. All this involves is a little bit of refactoring.
7+
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`.
88

99
from snippets.models import Snippet
1010
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
3030
return Response(serializer.data, status=status.HTTP_201_CREATED)
3131
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
3232

33-
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.
33+
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`.
3434

3535
class SnippetDetail(APIView):
3636
"""
@@ -62,7 +62,7 @@ So far, so good. It looks pretty similar to the previous case, but we've got be
6262

6363
That's looking good. Again, it's still pretty similar to the function based view right now.
6464

65-
We'll also need to refactor our URLconf slightly now we're using class based views.
65+
We'll also need to refactor our `urls.py` slightly now we're using class based views.
6666

6767
from django.conf.urls import patterns, url
6868
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
8383

8484
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.
8585

86-
Let's take a look at how we can compose our views by using the mixin classes.
86+
Let's take a look at how we can compose the views by using the mixin classes. Here's our `views.py` module again.
8787

8888
from snippets.models import Snippet
8989
from snippets.serializers import SnippetSerializer
@@ -126,7 +126,7 @@ Pretty similar. Again we're using the `GenericAPIView` class to provide the cor
126126

127127
## Using generic class based views
128128

129-
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.
129+
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.
130130

131131
from snippets.models import Snippet
132132
from snippets.serializers import SnippetSerializer

docs/tutorial/4-authentication-and-permissions.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Currently our API doesn't have any restrictions on who can edit or delete code s
1212
We're going to make a couple of changes to our `Snippet` model class.
1313
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.
1414

15-
Add the following two fields to the model.
15+
Add the following two fields to the `Snippet` model in `models.py`.
1616

1717
owner = models.ForeignKey('auth.User', related_name='snippets')
1818
highlighted = models.TextField()
@@ -52,7 +52,7 @@ You might also want to create a few different users, to use for testing the API.
5252

5353
## Adding endpoints for our User models
5454

55-
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:
55+
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:
5656

5757
from django.contrib.auth.models import User
5858

@@ -65,7 +65,7 @@ Now that we've got some users to work with, we'd better add representations of t
6565

6666
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.
6767

68-
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.
68+
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.
6969

7070
class UserList(generics.ListAPIView):
7171
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
7575
class UserDetail(generics.RetrieveAPIView):
7676
queryset = User.objects.all()
7777
serializer_class = UserSerializer
78+
79+
Make sure to also import the `UserSerializer` class
7880

79-
Finally we need to add those views into the API, by referencing them from the URL conf.
81+
from snippets.serializers import UserSerializer
82+
83+
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`.
8084

8185
url(r'^users/$', views.UserList.as_view()),
8286
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
9498

9599
## Updating our serializer
96100

97-
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:
101+
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`:
98102

99103
owner = serializers.Field(source='owner.username')
100104

rest_framework/serializers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,13 @@ def perform_validation(self, attrs):
262262
for field_name, field in self.fields.items():
263263
if field_name in self._errors:
264264
continue
265+
266+
source = field.source or field_name
267+
if self.partial and source not in attrs:
268+
continue
265269
try:
266270
validate_method = getattr(self, 'validate_%s' % field_name, None)
267271
if validate_method:
268-
source = field.source or field_name
269272
attrs = validate_method(attrs, source)
270273
except ValidationError as err:
271274
self._errors[field_name] = self._errors.get(field_name, []) + list(err.messages)
@@ -403,7 +406,7 @@ def field_from_native(self, data, files, field_name, into):
403406
return
404407

405408
# Set the serializer object if it exists
406-
obj = getattr(self.parent.object, field_name) if self.parent.object else None
409+
obj = get_component(self.parent.object, self.source or field_name) if self.parent.object else None
407410
obj = obj.all() if is_simple_callable(getattr(obj, 'all', None)) else obj
408411

409412
if self.source == '*':
@@ -912,7 +915,7 @@ def from_native(self, data, files):
912915

913916
def save_object(self, obj, **kwargs):
914917
"""
915-
Save the deserialized object and return it.
918+
Save the deserialized object.
916919
"""
917920
if getattr(obj, '_nested_forward_relations', None):
918921
# Nested relationships need to be saved before we can save the

rest_framework/templates/rest_framework/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@
110110

111111
<div class="content-main">
112112
<div class="page-header"><h1>{{ name }}</h1></div>
113+
{% block description %}
113114
{{ description }}
115+
{% endblock %}
114116
<div class="request-info" style="clear: both" >
115117
<pre class="prettyprint"><b>{{ request.method }}</b> {{ request.get_full_path }}</pre>
116118
</div>

rest_framework/tests/test_renderers.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_with_callback(self):
328328

329329
class YAMLRendererTests(TestCase):
330330
"""
331-
Tests specific to the JSON Renderer
331+
Tests specific to the YAML Renderer
332332
"""
333333

334334
def test_render(self):
@@ -354,6 +354,17 @@ def test_render_and_parse(self):
354354
data = parser.parse(StringIO(content))
355355
self.assertEqual(obj, data)
356356

357+
def test_render_decimal(self):
358+
"""
359+
Test YAML decimal rendering.
360+
"""
361+
renderer = YAMLRenderer()
362+
content = renderer.render({'field': Decimal('111.2')}, 'application/yaml')
363+
self.assertYAMLContains(content, "field: '111.2'")
364+
365+
def assertYAMLContains(self, content, string):
366+
self.assertTrue(string in content, '%r not in %r' % (string, content))
367+
357368

358369
class XMLRendererTestCase(TestCase):
359370
"""

rest_framework/tests/test_serializer.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,33 @@ def test_wrong_data(self):
511511
self.assertFalse(serializer.is_valid())
512512
self.assertEqual(serializer.errors, {'email': ['Enter a valid email address.']})
513513

514+
def test_partial_update(self):
515+
"""
516+
Make sure that validate_email isn't called when partial=True and email
517+
isn't found in data.
518+
"""
519+
initial_data = {
520+
'email': '[email protected]',
521+
'content': 'A test comment',
522+
'created': datetime.datetime(2012, 1, 1)
523+
}
524+
525+
serializer = self.CommentSerializerWithFieldValidator(data=initial_data)
526+
self.assertEqual(serializer.is_valid(), True)
527+
instance = serializer.object
528+
529+
new_content = 'An *updated* test comment'
530+
partial_data = {
531+
'content': new_content
532+
}
533+
534+
serializer = self.CommentSerializerWithFieldValidator(instance=instance,
535+
data=partial_data,
536+
partial=True)
537+
self.assertEqual(serializer.is_valid(), True)
538+
instance = serializer.object
539+
self.assertEqual(instance.content, new_content)
540+
514541

515542
class PositiveIntegerAsChoiceTests(TestCase):
516543
def test_positive_integer_in_json_is_correctly_parsed(self):

rest_framework/tests/test_serializer_nested.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,70 @@ def test_many_nested_validation_success(self):
244244
serializer = self.AlbumSerializer(data=data, many=True)
245245
self.assertEqual(serializer.is_valid(), True)
246246
self.assertEqual(serializer.object, expected_object)
247+
248+
249+
class ForeignKeyNestedSerializerUpdateTests(TestCase):
250+
def setUp(self):
251+
class Artist(object):
252+
def __init__(self, name):
253+
self.name = name
254+
255+
def __eq__(self, other):
256+
return self.name == other.name
257+
258+
class Album(object):
259+
def __init__(self, name, artist):
260+
self.name, self.artist = name, artist
261+
262+
def __eq__(self, other):
263+
return self.name == other.name and self.artist == other.artist
264+
265+
class ArtistSerializer(serializers.Serializer):
266+
name = serializers.CharField()
267+
268+
def restore_object(self, attrs, instance=None):
269+
if instance:
270+
instance.name = attrs['name']
271+
else:
272+
instance = Artist(attrs['name'])
273+
return instance
274+
275+
class AlbumSerializer(serializers.Serializer):
276+
name = serializers.CharField()
277+
by = ArtistSerializer(source='artist')
278+
279+
def restore_object(self, attrs, instance=None):
280+
if instance:
281+
instance.name = attrs['name']
282+
instance.artist = attrs['artist']
283+
else:
284+
instance = Album(attrs['name'], attrs['artist'])
285+
return instance
286+
287+
self.Artist = Artist
288+
self.Album = Album
289+
self.AlbumSerializer = AlbumSerializer
290+
291+
def test_create_via_foreign_key_with_source(self):
292+
"""
293+
Check that we can both *create* and *update* into objects across
294+
ForeignKeys that have a `source` specified.
295+
Regression test for #1170
296+
"""
297+
data = {
298+
'name': 'Discovery',
299+
'by': {'name': 'Daft Punk'},
300+
}
301+
302+
expected = self.Album(artist=self.Artist('Daft Punk'), name='Discovery')
303+
304+
# create
305+
serializer = self.AlbumSerializer(data=data)
306+
self.assertEqual(serializer.is_valid(), True)
307+
self.assertEqual(serializer.object, expected)
308+
309+
# update
310+
original = self.Album(artist=self.Artist('The Bats'), name='Free All the Monsters')
311+
serializer = self.AlbumSerializer(instance=original, data=data)
312+
self.assertEqual(serializer.is_valid(), True)
313+
self.assertEqual(serializer.object, expected)

rest_framework/utils/encoders.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ def represent_mapping(self, tag, mapping, flow_style=None):
8989
node.flow_style = best_style
9090
return node
9191

92+
SafeDumper.add_representer(decimal.Decimal,
93+
SafeDumper.represent_decimal)
94+
9295
SafeDumper.add_representer(SortedDict,
9396
yaml.representer.SafeRepresenter.represent_dict)
9497
SafeDumper.add_representer(DictWithMetadata,

0 commit comments

Comments
 (0)