Opened 18 years ago
Closed 18 years ago
#3661 closed (wontfix)
Serializers shoud have a option for serializing model properties
Reported by: | 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)
Change History (8)
comment:2 by , 18 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:3 by , 18 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 , 18 years ago
Has patch: | set |
---|
by , 18 years ago
Attachment: | serialize-attributes.2.patch added |
---|
Serialize properties in own dict in python/ json, own tag in xml
by , 18 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 , 18 years ago
Resolution: | → wontfix |
---|---|
Status: | new → 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.