Models and querysets are not serializeable using the default `json` tools, 
but Django provides its own set of tools that allow you to serialize models 
and querysets. Take a look at serializing django objects 
<https://docs.djangoproject.com/en/1.9/topics/serialization/#serializing-django-objects>
. 

On Saturday, December 12, 2015 at 5:41:16 PM UTC+1, Derek wrote:
>
> "Its not a bug, its a feature."
>
> See: 
> https://docs.djangoproject.com/en/1.9/ref/models/querysets/#when-querysets-are-evaluated
>
> If you were designing Django's internals, you might have decided that the 
> Queryset "should behave" in a certain way, but the designers have chosen 
> this approach.  There is no "disguise" about this; it's well-known and 
> well-documented.
>
>
> On Friday, 11 December 2015 15:43:45 UTC+2, Ws Hu wrote:
>>
>> import json
>> from myapp.models import MyModel
>>
>>
>> x = json.dumps(MyModel.objects.values_list('myField', flat=True)) # "[] 
>> is not JSON serializable"
>> x = json.dumps([x for x in MyModel.objects.values_list('myField', flat=
>> True)]) #ok
>>
>>
>>
>> ####
>> It seems that the database is accessed only if the result of 
>> objects.filter()/all()/values_list()/... is printed/iterated/... .
>> It's fine, but I think the methods of model manager like 
>> filter()/all()/values_list() should behave as if it is processing python 
>> objects. I have not yet digged into json module, but it did penetrated 
>> django's disguise.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1a244212-cb4a-4917-b6a4-901081e555d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to