Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#12845 closed (worksforme)

JSON serialization of empty query set raises "cannot serialize" error.

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


If I try to use

json.dumps({'items': myitemqueryset})

and the query set is empty, a serialization error comes up with the not-so-helpful message that [] cannot be serialized. In fact [] can be serialized. I take it that an empty queryset's repr returns [], but behaves differently than a normal empty list.

Attachments (0)

Change History (6)

comment:1 Changed 4 years ago by Alex

  • milestone set to 1.2
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by orokusaki

Oops. My mistake: I put json.dumps but meant:

{'items': serializers.serialize('json', myitemqueryset)}

comment:3 Changed 4 years ago by nessita

  • Owner changed from nobody to nessita
  • Status changed from new to assigned

This is working for me using SVN rev 12554, see working example:

In [3]: Person.objects.count()
Out[3]: 0

In [4]: from django.core import serializers

In [5]: serializers.serialize('json', Person.objects.all())
Out[5]: '[]'

comment:4 Changed 4 years ago by SmileyChris

  • Resolution set to worksforme
  • Status changed from assigned to closed

I concur:

In [1]: from django.core import serializers

In [2]: from django.contrib.auth.models import *

In [3]: Group.objects.all()
Out[3]: []

In [4]: serializers.serialize('json', Group.objects.all())
Out[4]: '[]'

In [5]: serializers.serialize('json', [])
Out[5]: '[]'

If there's some other factor we're failing to consider, orokusaki, please comment and reopen (after testing against the latest trunk)

comment:5 Changed 4 years ago by orokusaki

@SmileyChris, Alex I'm sorry, I didn't include my email so I didn't get an update. I will trust that I just had a typo or something of the sort, rather than trying to reopen this. I decided after much griping that week that I didn't need to try to use serializers for an API, and instead I've switched to Django Piston. Therefore I don't have the code anymore.

comment:6 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

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

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

Note: See TracTickets for help on using tickets.