Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#12845 closed (worksforme)

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

Reported by: Oroku Saki Owned by: Natalia Bidart
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.

Change History (6)

comment:1 Changed 8 years ago by Alex Gaynor

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by Oroku Saki

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

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

comment:3 Changed 8 years ago by Natalia Bidart

Owner: changed from nobody to Natalia Bidart
Status: newassigned

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 8 years ago by Chris Beaven

Resolution: worksforme
Status: assignedclosed

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 8 years ago by Oroku Saki

@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 7 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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