Code

Opened 7 years ago

Closed 7 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: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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@…> 7 years ago.
Patch for rev 4684
serialize-attributes.2.patch (7.5 KB) - added by Øyvind Saltvik <oyvind.saltvik@…> 7 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@…> 7 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 Changed 7 years ago by Øyvind Saltvik <oyvind@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
# 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 Changed 7 years ago by Simon G. <dev@…>

  • Triage Stage changed from Unreviewed to Design decision needed

Changed 7 years ago by Øyvind Saltvik <oyvind.saltvik@…>

Patch for rev 4684

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

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 Changed 7 years ago by Øyvind Saltvik <oyvind.saltvik@…>

  • Has patch set

Changed 7 years ago by Øyvind Saltvik <oyvind.saltvik@…>

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

Changed 7 years ago by Øyvind Saltvik <oyvind.saltvik@…>

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

comment:5 Changed 7 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.