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 page 
http://docs.djangoproject.com/en/dev/topics/serialization/#id2 there'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
-~----------~----~----~----~------~----~------~--~---

Reply via email to