mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Added a JSON serializer, a few more tests, and a couple more lines of docs.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3237 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
963d88a809
commit
c9032ab07f
7 changed files with 211 additions and 12 deletions
|
@ -91,4 +91,31 @@ API_TESTS = """
|
|||
>>> Article.objects.all()
|
||||
[<Article: Poker has no place on television>, <Article: Time to reform copyright>]
|
||||
|
||||
# Django also ships with a built-in JSON serializers
|
||||
>>> json = serializers.serialize("json", Category.objects.filter(pk=2))
|
||||
>>> json
|
||||
'[{"pk": "2", "model": "serializers.category", "fields": {"name": "Music"}}]'
|
||||
|
||||
# You can easily create new objects by deserializing data with an empty PK
|
||||
# (It's easier to demo this with JSON...)
|
||||
>>> new_author_json = '[{"pk": null, "model": "serializers.author", "fields": {"name": "Bill"}}]'
|
||||
>>> for obj in serializers.deserialize("json", new_author_json):
|
||||
... obj.save()
|
||||
>>> Author.objects.all()
|
||||
[<Author: Bill>, <Author: Jane>, <Author: Joe>]
|
||||
|
||||
# All the serializers work the same
|
||||
>>> json = serializers.serialize("json", Article.objects.all())
|
||||
>>> for obj in serializers.deserialize("json", json):
|
||||
... print obj
|
||||
<DeserializedObject: Poker has no place on television>
|
||||
<DeserializedObject: Time to reform copyright>
|
||||
|
||||
>>> json = json.replace("Poker has no place on television", "Just kidding; I love TV poker")
|
||||
>>> for obj in serializers.deserialize("json", json):
|
||||
... obj.save()
|
||||
|
||||
>>> Article.objects.all()
|
||||
[<Article: Just kidding; I love TV poker>, <Article: Time to reform copyright>]
|
||||
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue