Skip to content

Commit f9401f5

Browse files
committed
Fix Python 3 compat in documentation
1 parent de3929f commit f9401f5

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

docs/api-guide/serializers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ When deserializing data, you always need to call `is_valid()` before attempting
152152
serializer.is_valid()
153153
# False
154154
serializer.errors
155-
# {'email': [u'Enter a valid e-mail address.'], 'created': [u'This field is required.']}
155+
# {'email': ['Enter a valid e-mail address.'], 'created': ['This field is required.']}
156156

157157
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. The name of the `non_field_errors` key may be customized using the `NON_FIELD_ERRORS_KEY` REST framework setting.
158158

@@ -253,7 +253,7 @@ When passing data to a serializer instance, the unmodified data will be made ava
253253
By default, serializers must be passed values for all required fields or they will raise validation errors. You can use the `partial` argument in order to allow partial updates.
254254

255255
# Update `comment` with partial data
256-
serializer = CommentSerializer(comment, data={'content': u'foo bar'}, partial=True)
256+
serializer = CommentSerializer(comment, data={'content': 'foo bar'}, partial=True)
257257

258258
## Dealing with nested objects
259259

@@ -293,7 +293,7 @@ When dealing with nested representations that support deserializing the data, an
293293
serializer.is_valid()
294294
# False
295295
serializer.errors
296-
# {'user': {'email': [u'Enter a valid e-mail address.']}, 'created': [u'This field is required.']}
296+
# {'user': {'email': ['Enter a valid e-mail address.']}, 'created': ['This field is required.']}
297297

298298
Similarly, the `.validated_data` property will include nested data structures.
299299

@@ -415,7 +415,7 @@ You can provide arbitrary additional context by passing a `context` argument whe
415415

416416
serializer = AccountSerializer(account, context={'request': request})
417417
serializer.data
418-
# {'id': 6, 'owner': u'denvercoder9', 'created': datetime.datetime(2013, 2, 12, 09, 44, 56, 678870), 'details': 'http://example.com/accounts/6/details'}
418+
# {'id': 6, 'owner': 'denvercoder9', 'created': datetime.datetime(2013, 2, 12, 09, 44, 56, 678870), 'details': 'http://example.com/accounts/6/details'}
419419

420420
The context dictionary can be used within any serializer field logic, such as a custom `.to_representation()` method, by accessing the `self.context` attribute.
421421

@@ -1094,10 +1094,10 @@ This would then allow you to do the following:
10941094
>>> model = User
10951095
>>> fields = ('id', 'username', 'email')
10961096
>>>
1097-
>>> print UserSerializer(user)
1097+
>>> print(UserSerializer(user))
10981098
{'id': 2, 'username': 'jonwatts', 'email': '[email protected]'}
10991099
>>>
1100-
>>> print UserSerializer(user, fields=('id', 'email'))
1100+
>>> print(UserSerializer(user, fields=('id', 'email')))
11011101
{'id': 2, 'email': '[email protected]'}
11021102

11031103
## Customizing the default fields

docs/community/3.0-announcement.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ You can include `expiry_date` as a field option on a `ModelSerializer` class.
389389
These fields will be mapped to `serializers.ReadOnlyField()` instances.
390390

391391
>>> serializer = InvitationSerializer()
392-
>>> print repr(serializer)
392+
>>> print(repr(serializer))
393393
InvitationSerializer():
394394
to_email = EmailField(max_length=75)
395395
message = CharField(max_length=1000)

docs/tutorial/1-serialization.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,20 @@ Okay, once we've got a few imports out of the way, let's create a couple of code
137137
snippet = Snippet(code='foo = "bar"\n')
138138
snippet.save()
139139

140-
snippet = Snippet(code='print "hello, world"\n')
140+
snippet = Snippet(code='print("hello, world")\n')
141141
snippet.save()
142142

143143
We've now got a few snippet instances to play with. Let's take a look at serializing one of those instances.
144144

145145
serializer = SnippetSerializer(snippet)
146146
serializer.data
147-
# {'id': 2, 'title': u'', 'code': u'print "hello, world"\n', 'linenos': False, 'language': u'python', 'style': u'friendly'}
147+
# {'id': 2, 'title': '', 'code': 'print("hello, world")\n', 'linenos': False, 'language': 'python', 'style': 'friendly'}
148148

149149
At this point we've translated the model instance into Python native datatypes. To finalize the serialization process we render the data into `json`.
150150

151151
content = JSONRenderer().render(serializer.data)
152152
content
153-
# '{"id": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'
153+
# '{"id": 2, "title": "", "code": "print(\\"hello, world\\")\\n", "linenos": false, "language": "python", "style": "friendly"}'
154154

155155
Deserialization is similar. First we parse a stream into Python native datatypes...
156156

@@ -165,7 +165,7 @@ Deserialization is similar. First we parse a stream into Python native datatype
165165
serializer.is_valid()
166166
# True
167167
serializer.validated_data
168-
# OrderedDict([('title', ''), ('code', 'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])
168+
# OrderedDict([('title', ''), ('code', 'print("hello, world")\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])
169169
serializer.save()
170170
# <Snippet: Snippet object>
171171

@@ -175,7 +175,7 @@ We can also serialize querysets instead of model instances. To do so we simply
175175

176176
serializer = SnippetSerializer(Snippet.objects.all(), many=True)
177177
serializer.data
178-
# [OrderedDict([('id', 1), ('title', u''), ('code', u'foo = "bar"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 2), ('title', u''), ('code', u'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 3), ('title', u''), ('code', u'print "hello, world"'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])]
178+
# [OrderedDict([('id', 1), ('title', ''), ('code', 'foo = "bar"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 2), ('title', ''), ('code', 'print("hello, world")\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 3), ('title', ''), ('code', 'print("hello, world")'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])]
179179

180180
## Using ModelSerializers
181181

@@ -338,7 +338,7 @@ Finally, we can get a list of all of the snippets:
338338
{
339339
"id": 2,
340340
"title": "",
341-
"code": "print \"hello, world\"\n",
341+
"code": "print(\"hello, world\")\n",
342342
"linenos": false,
343343
"language": "python",
344344
"style": "friendly"
@@ -354,7 +354,7 @@ Or we can get a particular snippet by referencing its id:
354354
{
355355
"id": 2,
356356
"title": "",
357-
"code": "print \"hello, world\"\n",
357+
"code": "print(\"hello, world\")\n",
358358
"linenos": false,
359359
"language": "python",
360360
"style": "friendly"

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ We can get a list of all of the snippets, as before.
143143
{
144144
"id": 2,
145145
"title": "",
146-
"code": "print \"hello, world\"\n",
146+
"code": "print(\"hello, world\")\n",
147147
"linenos": false,
148148
"language": "python",
149149
"style": "friendly"
@@ -163,24 +163,24 @@ Or by appending a format suffix:
163163
Similarly, we can control the format of the request that we send, using the `Content-Type` header.
164164

165165
# POST using form data
166-
http --form POST http://127.0.0.1:8000/snippets/ code="print 123"
166+
http --form POST http://127.0.0.1:8000/snippets/ code="print(123)"
167167

168168
{
169169
"id": 3,
170170
"title": "",
171-
"code": "print 123",
171+
"code": "print(123)",
172172
"linenos": false,
173173
"language": "python",
174174
"style": "friendly"
175175
}
176176

177177
# POST using JSON
178-
http --json POST http://127.0.0.1:8000/snippets/ code="print 456"
178+
http --json POST http://127.0.0.1:8000/snippets/ code="print(456)"
179179

180180
{
181181
"id": 4,
182182
"title": "",
183-
"code": "print 456",
183+
"code": "print(456)",
184184
"linenos": false,
185185
"language": "python",
186186
"style": "friendly"

0 commit comments

Comments
 (0)