Hello, I'm also finding the built in serialization a bit overhead. It puts to much information in your JSON string that can be modified such as the PK field.
I often import json ( http://docs.python.org/library/json.html ) and serialize the data myself before passing it to the render method. This isn't the best solution but it works and I get a beautiful JSON string such as I wanted it to. Your idea of overriding the Serializer class sounds like a better idea to me. It should be possible (and will be interesting) ,I'll think I'll look into that tonight. Op 19-mei-2011, om 07:55 heeft redfive het volgende geschreven: > I'm trying to return some JSON from one of my views. My objects are > simple and I can get the built-in json serializer working, but wanted > a cleaner object when I deserialize in my webpage, i.e. I don't want > the pk or model entries. What is the best way to go about that? I > played around a little with overriding the end_object method in an > extension of the shipped JSONSerializer and was able to translate the > pk into the _current object (to reflect the pk as a member of the > object) but the resultant object still had pk/model and the fields > object that I had to traverse into. > > my models.py (snippet): > > Class Bug (models.Model): > id = models.IntegerField ( primary_key=True) > summary = models.CharField (max_length=256) > status = models.ForeignKey( Status, default=1 ) > > Class Status (models.Model): > objects = NaturalKeyManager() > value = models.TextField(unique=True, blank=True) > def natural_key(self): > return (self.value) > > my serializers.py: > > from django.core.serializers.json import Serializer as JSONSerializer > > Class Serializer: > def end_object(self, obj): > > # to allow bug.fields.bugId in the javascript > self._current['bugId'] = obj.id > > # append the object to the array of objects to be serialized > self.objects.append( self._current ) > > # clear the bug currently being processed so we don't mix up > data > self._current = None > > In JSON I'm looking for: > > { "buglist" : [ { "id" : 2, "summary" : "my summary", "status" : > "open" } ] } > > versus > > { "buglist" : [ { "pk" : 2, "model" : "my.models.Bug", "fields" : > [ "summary" : "my summary", "status" : "open"] } ] } > > > thanks for any guidance you can give. Perhaps there is a better way > entirely to handle JSON in django I haven't run across yet. > > -- redfive > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > Met vriendelijke groeten, Jonas Geiregat jo...@geiregat.org -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.