Ticket #3661: serialize-attributes.2.patch
File serialize-attributes.2.patch, 7.5 KB (added by , 18 years ago) |
---|
-
django/core/serializers/json.py
16 16 Convert a queryset to JSON. 17 17 """ 18 18 def end_serialization(self): 19 simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder, **self.options) 19 options = {} 20 for option in self.options: 21 if option in ['skipkeys', 'ensure_ascii', 'check_circular', 'allow_nan', 'cls', 'indent', 'encoding']: 22 options[option] = self.options[option] 23 simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder, **options) 20 24 21 25 def getvalue(self): 22 26 return self.stream.getvalue() -
django/core/serializers/xml_serializer.py
87 87 for relobj in getattr(obj, field.name).iterator(): 88 88 self.xml.addQuickElement("object", attrs={"pk" : str(relobj._get_pk_val())}) 89 89 self.xml.endElement("field") 90 90 91 def handle_property(self, obj, attr): 92 """ 93 Called to handle a model property 94 """ 95 self.xml.startElement("property", { 96 "name" : attr 97 }) 98 99 # Get a "string version" of the object's data (this is handled by the 100 # serializer base class). None is handled specially. 101 value = self.get_string_value(obj, attr) 102 if value is not None: 103 self.xml.characters(str(value)) 104 105 self.xml.endElement("property") 106 107 91 108 def _start_relational_field(self, field): 92 109 """ 93 110 Helper to output the <field> element for relational fields … … 208 225 inner_text.extend(getInnerText(child)) 209 226 else: 210 227 pass 211 return "".join(inner_text) 212 No newline at end of file 228 return "".join(inner_text) -
django/core/serializers/base.py
29 29 30 30 self.stream = options.get("stream", StringIO()) 31 31 self.selected_fields = options.get("fields") 32 self.serialize_attributes = options.get("attributes") 32 33 33 34 self.start_serialization() 34 35 for obj in queryset: … … 42 43 else: 43 44 if self.selected_fields is None or field.attname[:-3] in self.selected_fields: 44 45 self.handle_fk_field(obj, field) 46 if self.serialize_attributes: 47 attrdict = obj.__class__.__dict__ 48 for attr in attrdict: 49 if isinstance(attrdict[attr], property): 50 self.handle_property(obj, attr) 45 51 for field in obj._meta.many_to_many: 46 52 if self.selected_fields is None or field.attname in self.selected_fields: 47 53 self.handle_m2m_field(obj, field) … … 61 67 value = value.strftime("%Y-%m-%d %H:%M:%S") 62 68 elif isinstance(field, models.FileField): 63 69 value = getattr(obj, "get_%s_url" % field.name, lambda: None)() 70 elif isinstance(field, models.Field): 71 value = field.flatten_data(follow=None, obj=obj).get(field.name, "") 64 72 else: 65 value = field.flatten_data(follow=None, obj=obj).get(field.name, "")73 value = getattr(obj, field) 66 74 return str(value) 67 75 68 76 def start_serialization(self): -
django/core/serializers/python.py
21 21 pass 22 22 23 23 def start_object(self, obj): 24 self._current = { }24 self._current = {'fields': {}, 'properties': {}} 25 25 26 26 def end_object(self, obj): 27 self.objects.append({ 27 28 object = { 28 29 "model" : str(obj._meta), 29 30 "pk" : str(obj._get_pk_val()), 30 "fields" : self._current 31 }) 31 "fields" : self._current['fields'] 32 } 33 if self._current['properties']: 34 object.update({'properties': self._current['properties']}) 35 36 self.objects.append(object) 32 37 self._current = None 33 38 34 39 def handle_field(self, obj, field): 35 self._current[ field.name] = getattr(obj, field.name)40 self._current['fields'][field.name] = getattr(obj, field.name) 36 41 37 42 def handle_fk_field(self, obj, field): 38 43 related = getattr(obj, field.name) 39 44 if related is not None: 40 45 related = related._get_pk_val() 41 self._current[ field.name] = related46 self._current['fields'][field.name] = related 42 47 43 48 def handle_m2m_field(self, obj, field): 44 self._current[field.name] = [related._get_pk_val() for related in getattr(obj, field.name).iterator()] 45 49 self._current['fields'][field.name] = [related._get_pk_val() for related in getattr(obj, field.name).iterator()] 50 51 def handle_property(self, obj, attr): 52 self._current['properties'][attr] = getattr(obj, attr) 53 46 54 def getvalue(self): 47 55 return self.objects 48 56 -
tests/modeltests/serializers/models.py
40 40 class AuthorProfile(models.Model): 41 41 author = models.OneToOneField(Author) 42 42 date_of_birth = models.DateField() 43 44 def is_cool(self): 45 return True 46 is_cool = property(is_cool) 43 47 44 48 def __str__(self): 45 49 return "Profile of %s" % self.author … … 130 134 # pk identifier. 131 135 >>> profile = AuthorProfile(author=joe, date_of_birth=datetime(1970,1,1)) 132 136 >>> profile.save() 133 134 137 >>> json = serializers.serialize("json", AuthorProfile.objects.all()) 135 138 >>> json 136 139 '[{"pk": "1", "model": "serializers.authorprofile", "fields": {"date_of_birth": "1970-01-01"}}]' … … 139 142 ... print obj 140 143 <DeserializedObject: Profile of Joe> 141 144 145 # Optionally serialize model properties using, attributes=True, this is also affected by the fields option 146 >>> xml = serializers.serialize("xml", AuthorProfile.objects.all(), attributes=True) 147 >>> xml 148 '<?xml version="1.0" encoding="utf-8"?>\\n<django-objects version="1.0"><object pk="1" model="serializers.authorprofile"><field type="DateField" name="date_of_birth">1970-01-01</field><property name="is_cool">True</property></object></django-objects>' 149 >>> json = serializers.serialize("json", AuthorProfile.objects.all(), attributes=True) 150 >>> json 151 '[{"pk": "1", "model": "serializers.authorprofile", "properties": {"is_cool": true}, "fields": {"date_of_birth": "1970-01-01"}}]' 152 142 153 # Objects ids can be referenced before they are defined in the serialization data 143 154 # However, the deserialization process will need to be contained within a transaction 144 155 >>> json = '[{"pk": "3", "model": "serializers.article", "fields": {"headline": "Forward references pose no problem", "pub_date": "2006-06-16 15:00:00", "categories": [4, 1], "author": 4}}, {"pk": "4", "model": "serializers.category", "fields": {"name": "Reference"}}, {"pk": "4", "model": "serializers.author", "fields": {"name": "Agnes"}}]'