Thanks for your responses, they helped :) In the code below:
ret['b'] = State.objects.all() print 'ret: %' % ret try: serialized = encoder.JSONEncoder().encode(ret) except Exception, e: print 'exception: %s' % e I was doing the following mistake - I wanted to serialize query set in a dictionary directly, and the exception was thrown, I think because encoder.JSONEncoder() can't encode a QuerySet. While using code like this: ret = State.objects.all() json_serializer = serializers.get_serializer("json")() serialized_model = json_serializer.serialize(ret, ensure_ascii = False) ret_val['b'] = serialized_model serialized = encoder.JSONEncoder().encode(ret_val) then it doesn't throw exception, but code above just creates a serialized dicionary of serialized model, so when I try to decode it with JSON parser on a page, i get a dictionary of strings. I can create a JSON response manually by: serialized = '{"b":%s}'% serialized_model but it's a little annoying while serializing a few models. Is there any way to serialize dictionary of QuerySets without creating json response manually? Regards, Marek > 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 -~----------~----~----~----~------~----~------~--~---