You know what's weird? I've used simplejson.dumps() plenty of times in my own code... not sure why that one just slipped out of my memory. I should just stop responding to things :-)
Anyway, since you're serializing a model, you *should* be using your originally posted method. Use the way Marek said for anything other than models (of course, the object has to be serializable.) If that doesn't work, post the full (relevant) code. -Jeff On Mar 5, 7:01 am, Marek Wawrzyczek <mwawrzyc...@gmail.com> wrote: > Thomas Guettler wrote: > > > Jeff FW schrieb: > > >> The serializers are for serializing querysets/models. I'm surprised > >> you're not getting an error message there--are you catching all > >> exceptions? > > >> What you want is in django.utils.simplejson: > > >> from django.utils.simplejson import encoder > >> encoder.JSONEncoder().encode(ret) > > > Or this: > > >>>> from django.utils import simplejson > >>>> simplejson.dumps(...) > > > But unfortunately this does not encode datetime objects. > > > Thomas > > Thanks for your responses. Now when I try > > ret = { } > ret['a'] = 'b' > serialized = encoder.JSONEncoder().encode(ret) > print serialized > > then It works. > > But now I have another problem. I have a class "State": > > class State(models.Model): > state = models.CharField(max_length = 30) > > def __unicode__(self): > return self.state > > Throutht the admin page I create a state called "Slaskie". > Then the code : > > ret['b'] = State.objects.all() > print 'ret: %' % ret > try: > serialized = encoder.JSONEncoder().encode(ret) > except Exception, e: > print 'exception: %s' % e > > returns the output: > > {'b': [<State: Slaskie>]} > [<State: Slaskie>] is not JSON serializable > > At the > pagehttp://docs.djangoproject.com/en/dev/topics/serialization/#id2there's > written something about unicode and lazy translation. I tried to use > > le = LazyEncoder() #lazy encoder is a given class from the > link above > serialized = le.encode(ret) > > and then the exception was: > Circular reference detected > > when I tried > > le = LazyEncoder (ensure_ascii = False) > > the exception was the same: > Circular reference detected > > What's going on with this lazy translation and unicode ? How can I > serialize the data correctly ? > > Regards, > Marek --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---