Changeset 3237
- Timestamp:
- 06/29/06 11:42:49 (2 years ago)
- Files:
-
- django/trunk/django/core/serializers/base.py (modified) (1 diff)
- django/trunk/django/core/serializers/__init__.py (modified) (1 diff)
- django/trunk/django/core/serializers/json.py (added)
- django/trunk/django/core/serializers/python.py (added)
- django/trunk/django/core/serializers/xml_serializer.py (modified) (4 diffs)
- django/trunk/docs/serialization.txt (modified) (1 diff)
- django/trunk/tests/modeltests/serializers/models.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/serializers/base.py
r3225 r3237 34 34 self.start_object(obj) 35 35 for field in obj._meta.fields: 36 if field.rel is None: 36 if field is obj._meta.pk: 37 continue 38 elif field.rel is None: 37 39 self.handle_field(obj, field) 38 40 else: django/trunk/django/core/serializers/__init__.py
r3225 r3237 21 21 # Built-in serializers 22 22 BUILTIN_SERIALIZERS = { 23 "xml" : "django.core.serializers.xml_serializer", 23 "xml" : "django.core.serializers.xml_serializer", 24 "python" : "django.core.serializers.python", 25 "json" : "django.core.serializers.json", 24 26 } 25 27 django/trunk/django/core/serializers/xml_serializer.py
r3225 r3237 3 3 """ 4 4 5 from xml.dom import pulldom 6 from django.utils.xmlutils import SimplerXMLGenerator 5 from django.conf import settings 7 6 from django.core.serializers import base 8 7 from django.db import models 8 from django.utils.xmlutils import SimplerXMLGenerator 9 from xml.dom import pulldom 9 10 10 11 class Serializer(base.Serializer): … … 17 18 Start serialization -- open the XML document and the root element. 18 19 """ 19 self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", "utf-8"))20 self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", settings.DEFAULT_CHARSET)) 20 21 self.xml.startDocument() 21 22 self.xml.startElement("django-objects", {"version" : "1.0"}) … … 59 60 # serializer base class). None is handled specially. 60 61 value = self.get_string_value(obj, field) 61 if value is None: 62 self.xml.addQuickElement("None") 63 else: 62 if value is not None: 64 63 self.xml.characters(str(value)) 65 64 … … 107 106 def __init__(self, stream_or_string, **options): 108 107 super(Deserializer, self).__init__(stream_or_string, **options) 109 self.encoding = self.options.get("encoding", "utf-8")108 self.encoding = self.options.get("encoding", settings.DEFAULT_CHARSET) 110 109 self.event_stream = pulldom.parse(self.stream) 111 110 django/trunk/docs/serialization.txt
r3225 r3237 79 79 --------------------- 80 80 81 Django "ships" with a few included serializers , and there's a simple API for creating and registering your own...81 Django "ships" with a few included serializers: 82 82 83 .. note:: 83 ========== ============================================================== 84 Identifier Information 85 ========== ============================================================== 86 ``xml`` Serializes to and from a simple XML dialect. 84 87 85 ... which will be documented once the API is stable :) 88 ``json`` Serializes to and from JSON_ (using a version of simplejson_ 89 bundled with Django). 90 91 ``python`` Translates to and from "simple" Python objects (lists, dicts, 92 strings, etc.). Not really all that useful on its own, but 93 used as a base for other serializers. 94 ========== ============================================================== 95 96 .. _json: http://json.org/ 97 .. _simplejson: http://undefined.org/python/#simplejson 98 99 Writing custom serializers 100 `````````````````````````` 101 102 XXX ... django/trunk/tests/modeltests/serializers/models.py
r3225 r3237 92 92 [<Article: Poker has no place on television>, <Article: Time to reform copyright>] 93 93 94 # Django also ships with a built-in JSON serializers 95 >>> json = serializers.serialize("json", Category.objects.filter(pk=2)) 96 >>> json 97 '[{"pk": "2", "model": "serializers.category", "fields": {"name": "Music"}}]' 98 99 # You can easily create new objects by deserializing data with an empty PK 100 # (It's easier to demo this with JSON...) 101 >>> new_author_json = '[{"pk": null, "model": "serializers.author", "fields": {"name": "Bill"}}]' 102 >>> for obj in serializers.deserialize("json", new_author_json): 103 ... obj.save() 104 >>> Author.objects.all() 105 [<Author: Bill>, <Author: Jane>, <Author: Joe>] 106 107 # All the serializers work the same 108 >>> json = serializers.serialize("json", Article.objects.all()) 109 >>> for obj in serializers.deserialize("json", json): 110 ... print obj 111 <DeserializedObject: Poker has no place on television> 112 <DeserializedObject: Time to reform copyright> 113 114 >>> json = json.replace("Poker has no place on television", "Just kidding; I love TV poker") 115 >>> for obj in serializers.deserialize("json", json): 116 ... obj.save() 117 118 >>> Article.objects.all() 119 [<Article: Just kidding; I love TV poker>, <Article: Time to reform copyright>] 120 94 121 """
