Opened 7 years ago

Closed 7 years ago

Last modified 2 months ago

#8090 closed Uncategorized (invalid)

Problem in serializing the list returned from objects.values_list()

Reported by: shavin Owned by: nobody
Component: Core (Serialization) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by ramiro)

#when i run this code:

from StudentsSite.Names.models import *
from django.utils import simplejson

lstSchoolCertificateNames = SchoolCertificateName.objects.values_list('school_certificate_name', flat=True)

serializedlist=simplejson.dumps(lstSchoolCertificateNames, encoding='utf-8');

#this is given as error:

Environment:

Request Method: GET
Request URL: http://localhost:8000/StudentsSite/Students/Search/
Django Version: 1.0-alpha-SVN-8161
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'StudentsSite.Students',
 'django.contrib.admin',
 'django.contrib.databrowse',
 'StudentsSite.Names']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File "C:\Python25\lib\site-packages\django\core\handlers\base.py" in get_response
  87.                 response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\shavin\django\StudentsSite\..\StudentsSite\Students\views.py" in searchForm
  37.         lstSchoolCertificateNames2=simplejson.dumps(lstSchoolCertificateNames, encoding='utf-8');
File "C:\Python25\lib\site-packages\django\utils\simplejson\__init__.py" in dumps
  236.         return _default_encoder.encode(obj)
File "C:\Python25\lib\site-packages\django\utils\simplejson\encoder.py" in encode
  366.         chunks = list(self.iterencode(o))
File "C:\Python25\lib\site-packages\django\utils\simplejson\encoder.py" in _iterencode
  316.             for chunk in self._iterencode_default(o, markers):
File "C:\Python25\lib\site-packages\django\utils\simplejson\encoder.py" in _iterencode_default
  322.         newobj = self.default(o)
File "C:\Python25\lib\site-packages\django\utils\simplejson\encoder.py" in default
  343.         raise TypeError("%r is not JSON serializable" % (o,))

Exception Type: TypeError at /StudentsSite/Students/Search/
Exception Value: [u'icse'] is not JSON serializable

#AND when this is done

from StudentsSite.Names.models import *
from django.core.serializers import serialize

lstSchoolCertificateNames = SchoolCertificateName.objects.values_list('school_certificate_name', flat=True)

serializedlist=serialize('json',lstSchoolCertificateNames, encoding='utf-8')


#it gives this error:

Environment:

Request Method: GET
Request URL: http://localhost:8000/StudentsSite/Students/Search/
Django Version: 1.0-alpha-SVN-8161
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'StudentsSite.Students',
 'django.contrib.admin',
 'django.contrib.databrowse',
 'StudentsSite.Names']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File "C:\Python25\lib\site-packages\django\core\handlers\base.py" in get_response
  87.                 response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\shavin\django\StudentsSite\..\StudentsSite\Students\views.py" in searchForm
  38.         lstSchoolCertificateNames2=serialize('json', lstSchoolCertificateNames)
File "C:\Python25\lib\site-packages\django\core\serializers\__init__.py" in serialize
  72.     s.serialize(queryset, **options)
File "C:\Python25\lib\site-packages\django\core\serializers\base.py" in serialize
  40.             for field in obj._meta.local_fields:

Exception Type: AttributeError at /StudentsSite/Students/Search/
Exception Value: 'unicode' object has no attribute '_meta'

#and i also found that values_list() function was returning an object of the type <django.db.models.query.ValuesListQuerySet >

Change History (4)

comment:1 Changed 7 years ago by shavin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by ramiro

  • Description modified (diff)
  • Version changed from 1.0-alpha to SVN

Reformatted description, set Version field to SVN (as per the Django Version: 1.0-alpha-SVN-8161 fragment)

comment:3 Changed 7 years ago by russellm

  • Resolution set to invalid
  • Status changed from new to closed

The reported error message is correctly in both cases. The Simplejson library can only handle python primitive types; values_list() returns a ValuesListQuerySet, so an error is raised. If you convert the ValuesListQuerySet into a list using list(), the problem is resolved. The Django serialization module only works on lists/querysets of full Django objects; ValuesListQuerySet contains tuples, not Django objects.

comment:4 Changed 2 months ago by deryckh

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

I know this is an old bug, but it's still an issue with current Django, as of 1.7 at least. I know Russell closed this on the grounds that a ValuesListQuerySet is not a list, but I do think it's a bug that you can indeed use a flat ValuesListQuerySet like a list everywhere except when passing it to the json serializer. If it looks like a list, it ought to act like a list, no?

Note: See TracTickets for help on using tickets.
Back to Top