#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: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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 by , 15 years ago
milestone: | → 1.2 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 15 years ago
comment:3 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | new → 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 by , 15 years ago
Resolution: | → worksforme |
---|---|
Status: | assigned → 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 by , 15 years ago
@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.
Oops. My mistake: I put json.dumps but meant: