How can this be that this error was raised? I entered this:
def json(self):
return json.dumps(
{
'items': self.items
}
)
and got that error (because self.items was an empty queryset (Django)
but then,
def json(self):
return json.dumps(
{
'items': [] # Pass in empty list to prove that the error was idiotic.
}
)
worked fine (which at least proves that the error message is worthless)
Is this because the queryset defines repr() and returns ‘[]’ as a string when it’s empty or something ridiculous like that?
Querysets are not serializable out-of-the-box. If you try
list(self.items)instead of justself.items, that should work as long as the items themselves are JSON-serializable.Update: It will raise an exception even if it isn’t empty. I don’t think it’ll be accepted as a Django bug, though of course you can try; the simplest answer is to force evaluation using
list(qs), as I’ve already said.