mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-26 12:03:44 +03:00
Update documentation
This commit is contained in:
parent
760da25c60
commit
a0a601301f
|
@ -562,11 +562,13 @@ will take some serious design work.</p>
|
||||||
<p>The serializers in REST framework work very similarly to Django's <code>Form</code> and <code>ModelForm</code> classes. We provide a <code>Serializer</code> class which gives you a powerful, generic way to control the output of your responses, as well as a <code>ModelSerializer</code> class which provides a useful shortcut for creating serializers that deal with model instances and querysets.</p>
|
<p>The serializers in REST framework work very similarly to Django's <code>Form</code> and <code>ModelForm</code> classes. We provide a <code>Serializer</code> class which gives you a powerful, generic way to control the output of your responses, as well as a <code>ModelSerializer</code> class which provides a useful shortcut for creating serializers that deal with model instances and querysets.</p>
|
||||||
<h2 id="declaring-serializers">Declaring Serializers</h2>
|
<h2 id="declaring-serializers">Declaring Serializers</h2>
|
||||||
<p>Let's start by creating a simple object we can use for example purposes:</p>
|
<p>Let's start by creating a simple object we can use for example purposes:</p>
|
||||||
<pre><code>class Comment(object):
|
<pre><code>from datetime import datetime
|
||||||
|
|
||||||
|
class Comment(object):
|
||||||
def __init__(self, email, content, created=None):
|
def __init__(self, email, content, created=None):
|
||||||
self.email = email
|
self.email = email
|
||||||
self.content = content
|
self.content = content
|
||||||
self.created = created or datetime.datetime.now()
|
self.created = created or datetime.now()
|
||||||
|
|
||||||
comment = Comment(email='leila@example.com', content='foo bar')
|
comment = Comment(email='leila@example.com', content='foo bar')
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
@ -594,10 +596,10 @@ json
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<h2 id="deserializing-objects">Deserializing objects</h2>
|
<h2 id="deserializing-objects">Deserializing objects</h2>
|
||||||
<p>Deserialization is similar. First we parse a stream into Python native datatypes...</p>
|
<p>Deserialization is similar. First we parse a stream into Python native datatypes...</p>
|
||||||
<pre><code>from StringIO import StringIO
|
<pre><code>from django.utils.six import BytesIO
|
||||||
from rest_framework.parsers import JSONParser
|
from rest_framework.parsers import JSONParser
|
||||||
|
|
||||||
stream = StringIO(json)
|
stream = BytesIO(json)
|
||||||
data = JSONParser().parse(stream)
|
data = JSONParser().parse(stream)
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>...then we restore those native datatypes into a dictionary of validated data.</p>
|
<p>...then we restore those native datatypes into a dictionary of validated data.</p>
|
||||||
|
|
|
@ -533,9 +533,7 @@ content
|
||||||
# '{"pk": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'
|
# '{"pk": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>Deserialization is similar. First we parse a stream into Python native datatypes...</p>
|
<p>Deserialization is similar. First we parse a stream into Python native datatypes...</p>
|
||||||
<pre><code># This import will use either `StringIO.StringIO` or `io.BytesIO`
|
<pre><code>from django.utils.six import BytesIO
|
||||||
# as appropriate, depending on if we're running Python 2 or Python 3.
|
|
||||||
from django.utils.six import BytesIO
|
|
||||||
|
|
||||||
stream = BytesIO(content)
|
stream = BytesIO(content)
|
||||||
data = JSONParser().parse(stream)
|
data = JSONParser().parse(stream)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user