@@ -150,7 +150,7 @@ At this point we've translated the model instance into Python native datatypes.
150
150
151
151
content = JSONRenderer().render(serializer.data)
152
152
content
153
- # b'{"id": 2, "title": "", "code": "print(\\"hello, world\\")\\n", "linenos": false, "language": "python", "style": "friendly"}'
153
+ # b'{"id":2, "title":"","code":"print(\\"hello, world\\")\\n","linenos":false,"language":"python","style":"friendly"}'
154
154
155
155
Deserialization is similar. First we parse a stream into Python native datatypes...
156
156
@@ -165,7 +165,7 @@ Deserialization is similar. First we parse a stream into Python native datatype
165
165
serializer.is_valid()
166
166
# True
167
167
serializer.validated_data
168
- # OrderedDict([( 'title', ''), ( 'code', 'print("hello, world")\n'), ( 'linenos', False), ( 'language', 'python'), ( 'style', 'friendly')])
168
+ # { 'title': '', 'code': 'print("hello, world")', 'linenos': False, 'language': 'python', 'style': 'friendly'}
169
169
serializer.save()
170
170
# <Snippet: Snippet object>
171
171
@@ -175,7 +175,7 @@ We can also serialize querysets instead of model instances. To do so we simply
175
175
176
176
serializer = SnippetSerializer(Snippet.objects.all(), many=True)
177
177
serializer.data
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')]) ]
178
+ # [{ 'id': 1, 'title': '', 'code': 'foo = "bar"\n', 'linenos': False, 'language': 'python', 'style': 'friendly'}, { 'id': 2, 'title': '', 'code': 'print("hello, world")\n', 'linenos': False, 'language': 'python', 'style': 'friendly'}, { 'id': 3, 'title': '', 'code': 'print("hello, world")', 'linenos': False, 'language': 'python', 'style': 'friendly'} ]
179
179
180
180
## Using ModelSerializers
181
181
@@ -321,42 +321,50 @@ You can install httpie using pip:
321
321
322
322
Finally, we can get a list of all of the snippets:
323
323
324
- http http://127.0.0.1:8000/snippets/
324
+ http http://127.0.0.1:8000/snippets/ --unsorted
325
325
326
326
HTTP/1.1 200 OK
327
327
...
328
328
[
329
- {
330
- "id": 1,
331
- "title": "",
332
- "code": "foo = \"bar\"\n",
333
- "linenos": false,
334
- "language": "python",
335
- "style": "friendly"
336
- },
337
- {
338
- "id": 2,
339
- "title": "",
340
- "code": "print(\"hello, world\")\n",
341
- "linenos": false,
342
- "language": "python",
343
- "style": "friendly"
344
- }
329
+ {
330
+ "id": 1,
331
+ "title": "",
332
+ "code": "foo = \"bar\"\n",
333
+ "linenos": false,
334
+ "language": "python",
335
+ "style": "friendly"
336
+ },
337
+ {
338
+ "id": 2,
339
+ "title": "",
340
+ "code": "print(\"hello, world\")\n",
341
+ "linenos": false,
342
+ "language": "python",
343
+ "style": "friendly"
344
+ },
345
+ {
346
+ "id": 3,
347
+ "title": "",
348
+ "code": "print(\"hello, world\")",
349
+ "linenos": false,
350
+ "language": "python",
351
+ "style": "friendly"
352
+ }
345
353
]
346
354
347
355
Or we can get a particular snippet by referencing its id:
348
356
349
- http http://127.0.0.1:8000/snippets/2/
357
+ http http://127.0.0.1:8000/snippets/2/ --unsorted
350
358
351
359
HTTP/1.1 200 OK
352
360
...
353
361
{
354
- "id": 2,
355
- "title": "",
356
- "code": "print(\"hello, world\")\n",
357
- "linenos": false,
358
- "language": "python",
359
- "style": "friendly"
362
+ "id": 2,
363
+ "title": "",
364
+ "code": "print(\"hello, world\")\n",
365
+ "linenos": false,
366
+ "language": "python",
367
+ "style": "friendly"
360
368
}
361
369
362
370
Similarly, you can have the same json displayed by visiting these URLs in a web browser.
0 commit comments