Skip to content

Commit

Permalink
docs: Correct some evaluation results and a httpie option in Tutorial1 (
Browse files Browse the repository at this point in the history
#9421)

* Tutorial 1: Added --unsorted option to httpie calls to prevent automatic json key sorting

* Tutorial 1: Changed evaluation results accurate
  • Loading branch information
wkwkhautbois committed Jun 2, 2024
1 parent 36d5c0e commit fbdab09
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions docs/tutorial/1-serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ At this point we've translated the model instance into Python native datatypes.

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

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

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

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

serializer = SnippetSerializer(Snippet.objects.all(), many=True)
serializer.data
# [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')])]
# [{'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'}]

## Using ModelSerializers

Expand Down Expand Up @@ -321,42 +321,50 @@ You can install httpie using pip:

Finally, we can get a list of all of the snippets:

http http://127.0.0.1:8000/snippets/
http http://127.0.0.1:8000/snippets/ --unsorted

HTTP/1.1 200 OK
...
[
{
"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": 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"
}
]

Or we can get a particular snippet by referencing its id:

http http://127.0.0.1:8000/snippets/2/
http http://127.0.0.1:8000/snippets/2/ --unsorted

HTTP/1.1 200 OK
...
{
"id": 2,
"title": "",
"code": "print(\"hello, world\")\n",
"linenos": false,
"language": "python",
"style": "friendly"
"id": 2,
"title": "",
"code": "print(\"hello, world\")\n",
"linenos": false,
"language": "python",
"style": "friendly"
}

Similarly, you can have the same json displayed by visiting these URLs in a web browser.
Expand Down

0 comments on commit fbdab09

Please sign in to comment.