Django

Code

Changeset 5409

Show
Ignore:
Timestamp:
06/01/07 08:39:08 (1 year ago)
Author:
russellm
Message:

Fixed #3466 -- Fixed problem with specifyin a 'fields' argument to a JSON serializer. Also added documenation for the 'fields' argument.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/serializers/json.py

    r5311 r5409  
    2222    """ 
    2323    def end_serialization(self): 
     24        self.options.pop('stream', None) 
     25        self.options.pop('fields', None) 
    2426        simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options) 
    2527 
  • django/trunk/django/core/serializers/pyyaml.py

    r4734 r5409  
    1919    """ 
    2020    def end_serialization(self): 
     21        self.options.pop('stream', None) 
     22        self.options.pop('fields', None) 
    2123        yaml.dump(self.objects, self.stream, **self.options) 
    2224         
  • django/trunk/docs/serialization.txt

    r5165 r5409  
    4444 
    4545.. _HTTPResponse: ../request_response/#httpresponse-objects 
     46 
     47Subset of fields 
     48~~~~~~~~~~~~~~~~ 
     49 
     50If you only want a subset of fields to be serialized, you can  
     51specify a `fields` argument to the serializer:: 
     52 
     53    from django.core import serializers 
     54    data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size')) 
     55 
     56In this example, only the `name` and `size` attributes of each model will 
     57be serialized.  
     58 
     59.. note:: 
     60 
     61    Depending on your model, you may find that it is not possible to deserialize 
     62    a model that only serializes a subset of it's fields. If a serialized object 
     63    doesn't specify all the fields that are required by a model, the deserializer 
     64    will not be able to save deserialized instances. 
    4665 
    4766Deserializing data 
     
    93112                strings, etc.).  Not really all that useful on its own, but 
    94113                used as a base for other serializers. 
     114     
     115    ``yaml``    Serializes to YAML (Yet Another Markup Lanuage). This  
     116                serializer will only be made available if PyYAML_ is installed. 
     117                 
    95118    ==========  ============================================================== 
    96119 
    97120.. _json: http://json.org/ 
    98121.. _simplejson: http://undefined.org/python/#simplejson 
     122.. _PyYAML: http://www.pyyaml.org/ 
    99123 
    100124Notes for specific serialization formats 
  • django/trunk/tests/modeltests/serializers/models.py

    r4796 r5409  
    160160<Author: Agnes> 
    161161 
     162# Serializer output can be restricted to a subset of fields 
     163>>> print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date')) 
     164[{"pk": "1", "model": "serializers.article", "fields": {"headline": "Just kidding; I love TV poker", "pub_date": "2006-06-16 11:00:00"}}, {"pk": "2", "model": "serializers.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": "3", "model": "serializers.article", "fields": {"headline": "Forward references pose no problem", "pub_date": "2006-06-16 15:00:00"}}] 
     165 
    162166"""} 
  • django/trunk/tests/regressiontests/serializers_regress/models.py

    r5371 r5409  
    206206#     data = models.XMLField(primary_key=True) 
    207207 
     208class ComplexModel(models.Model): 
     209    field1 = models.CharField(maxlength=10) 
     210    field2 = models.CharField(maxlength=10) 
     211    field3 = models.CharField(maxlength=10) 
  • django/trunk/tests/regressiontests/serializers_regress/tests.py

    r5371 r5409  
    1010 
    1111import unittest, datetime 
     12from cStringIO import StringIO 
    1213 
    1314from django.utils.functional import curry 
     
    279280        func[1](self, pk, klass, datum) 
    280281 
     282def fieldsTest(format, self): 
     283    # Clear the database first 
     284    management.flush(verbosity=0, interactive=False) 
     285 
     286    obj = ComplexModel(field1='first',field2='second',field3='third') 
     287    obj.save() 
     288     
     289    # Serialize then deserialize the test database 
     290    serialized_data = serializers.serialize(format, [obj], indent=2, fields=('field1','field3')) 
     291    result = serializers.deserialize(format, serialized_data).next() 
     292     
     293    # Check that the deserialized object contains data in only the serialized fields. 
     294    self.assertEqual(result.object.field1, 'first') 
     295    self.assertEqual(result.object.field2, '') 
     296    self.assertEqual(result.object.field3, 'third') 
     297 
     298def streamTest(format, self): 
     299    # Clear the database first 
     300    management.flush(verbosity=0, interactive=False) 
     301 
     302    obj = ComplexModel(field1='first',field2='second',field3='third') 
     303    obj.save() 
     304     
     305    # Serialize the test database to a stream 
     306    stream = StringIO()     
     307    serializers.serialize(format, [obj], indent=2, stream=stream) 
     308     
     309    # Serialize normally for a comparison 
     310    string_data = serializers.serialize(format, [obj], indent=2) 
     311 
     312    # Check that the two are the same 
     313    self.assertEqual(string_data, stream.buffer()) 
     314    stream.close() 
     315     
    281316for format in serializers.get_serializer_formats(): 
    282317    setattr(SerializerTests, 'test_'+format+'_serializer', curry(serializerTest, format)) 
     318    setattr(SerializerTests, 'test_'+format+'_serializer_fields', curry(fieldsTest, format)) 
     319    setattr(SerializerTests, 'test_'+format+'_serializer_stream', curry(fieldsTest, format))