Code

Opened 6 years ago

Closed 6 years ago

#8090 closed (invalid)

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

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

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 >

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by shavin

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

comment:2 Changed 6 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 6 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.