开发者

Passing JSON with Django

开发者 https://www.devze.com 2023-04-09 04:45 出处:网络
I\'m passing information to a Processing sketch from a Django backend.The problem arises with \"lectures\", it seems that it is not serializable.I\'m missing something very basic methinks.

I'm passing information to a Processing sketch from a Django backend. The problem arises with "lectures", it seems that it is not serializable. I'm missing something very basic methinks.

def get(request, last_update = None):
    timestamp = time.time() 
    if last_update:
        last_update = datetime.fromtimestamp(float(last_update))
        lectures = Lecture.objects.all().filter(datetime_edit__gt=last_update)
    else:
        lectures = Lecture.objects.all()
    updates = {'timestamp': timestamp, 'lectures': lectures}
    print updates
    return HttpResponse(json.dumps(updates), mimetype="application/json")

Here is the response that I'm getting in the browser.

[<Lecture: whale开发者_运维知识库s>, <Lecture: cats>, <Lecture: rain>, <Lecture: china>] is not JSON serializable


QuerySet cannot be serialized in this manner. Use .values() and list() to turn it into a basic Python structure (i.e. a list of dicts) first.


The JSON serialize does not support serialization of arbitrary class instances. You should convert Lecture records into dictionaries before serializing.

0

精彩评论

暂无评论...
验证码 换一张
取 消