Opened 17 years ago

Closed 17 years ago

#3661 closed (wontfix)

Serializers shoud have a option for serializing model properties

Reported by: Øyvind Saltvik <oyvind@…> Owned by: Jacob
Component: Core (Serialization) Version: dev
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

class MyModel(models.Model):

    mydescription = models.CharField(maxlength=50)

    def myattr(self):
        return 'this is a %s' % self.mydescription

    myattr = property(myattr)


should be able to optionally serialize this to {"mydescription": "test", "myattr": "this is a test"}

Attachments (3)

serialize-attributes.patch (5.6 KB ) - added by Øyvind Saltvik <oyvind.saltvik@…> 17 years ago.
Patch for rev 4684
serialize-attributes.2.patch (7.5 KB ) - added by Øyvind Saltvik <oyvind.saltvik@…> 17 years ago.
Serialize properties in own dict in python/ json, own tag in xml
serialize-properties.diff (8.6 KB ) - added by Øyvind Saltvik <oyvind.saltvik@…> 17 years ago.
fixed for yaml, test for yaml, fix for invalid kwargs like fields and attributes passed yaml.dump and simplejson.dump

Download all attachments as: .zip

Change History (8)

comment:1 by Øyvind Saltvik <oyvind@…>, 17 years ago

# something like this

def prop_iter(qs):
    outlist = []
    for i in qs:
        modeldict = i.__class__.__dict__
        datadict = i.__dict__
        for d in modeldict:
            if isinstance(modeldict[d], property):
                datadict[d] = getattr(i, d)
        outlist += [datadict]
    return outlist

comment:2 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedDesign decision needed

by Øyvind Saltvik <oyvind.saltvik@…>, 17 years ago

Attachment: serialize-attributes.patch added

Patch for rev 4684

comment:3 by Øyvind Saltvik <oyvind.saltvik@…>, 17 years ago

Patch with two tests, also fixes non valid options like field and attributes being passed to simplejson dumps.

Usage

from django.core import serializers
json = serializers.serialise("json", queryset.all(), attributes=True)

}}

comment:4 by Øyvind Saltvik <oyvind.saltvik@…>, 17 years ago

Has patch: set

by Øyvind Saltvik <oyvind.saltvik@…>, 17 years ago

Serialize properties in own dict in python/ json, own tag in xml

by Øyvind Saltvik <oyvind.saltvik@…>, 17 years ago

Attachment: serialize-properties.diff added

fixed for yaml, test for yaml, fix for invalid kwargs like fields and attributes passed yaml.dump and simplejson.dump

comment:5 by Russell Keith-Magee, 17 years ago

Resolution: wontfix
Status: newclosed

I'm afraid I don't see the benefit of what you are proposing. The
serialization framework exists for the easy serialization of Django
DB-backed objects - not for the arbitrary serialization of _any_
object, and derived properties like the ones you are highlighting as
examples don't add anything to the serialized representation of a
DB-backed object.

For the use-case you describe - XMLRPC and similar calls - you need to
adhere to a specific API. It would expect it to be very unusual for
the requirements of an RPC API to match the serialization strategy of
a Django object. For these problems, you need to serialize a very
specific set of object-related information (not the object itself) in
a very specific format; the better approach in this cases would be to
construct a dictionary that explicitly contains the data you want, and
use the SimpleJSON/PyYAML/XML libraries natively to serialize the
dictionary into the desired format.

Note: See TracTickets for help on using tickets.
Back to Top