a bug in model.Manager.values_list?

29 views
Skip to first unread message

Ws Hu

unread,
Dec 11, 2015, 8:43:45 AM12/11/15
to Django users
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.

Derek

unread,
Dec 12, 2015, 11:41:16 AM12/12/15
to Django users
"Its not a bug, its a feature."


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.

knbk

unread,
Dec 12, 2015, 12:12:00 PM12/12/15
to Django users
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
Reply all
Reply to author
Forward
0 new messages