Django的返回单个纪录JSON [英] Django return a single record as JSON

查看:103
本文介绍了Django的返回单个纪录JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加有关我们的Web管理客户的笔记创建的窗体。我使用jQuery和Ajax提交的。我想Django的视图返回新添加的音符/记录,所以我可以将其追加到客户注释表。我的阿贾克斯发送的工作,并说明被保存,我只是不能显示的结果。

I created a form for adding notes about a customer in our web admin. I am using jQuery and Ajax to submit the for. I would like the Django view to return the newly added note/record so I can append it to the customer notes table. My Ajax send is working, and the note is being saved, I just can't display the result.

我曾尝试下面3条线(单独):

I have tried the following 3 lines (separately):

serializers.serialize("json", Note.objects.get(id=new_note.id))
serializers.serialize("json", new_note)
return HttpResponse(simplejson.dumps(new_note), mimetype='application/javascript')

第2各生产:

The first 2 each produce:

'Note' object is not iterable

和第三人给我:

<Note: Note object> is not JSON serializable

我不真正关心其格式我返回对象,只要我能接收并显示使用jQuery记录的每个领域。

I don't actually care in which format I return the object, as long as I can receive and display each field of the record using jQuery.

感谢。

推荐答案

的文档< /一>:

From the docs:

的参数序列化功能是序列化数据(见序列化格式)和一个QuerySet序列化格式。

The arguments to the serialize function are the format to serialize the data to (see Serialization formats) and a QuerySet to serialize.

使用过滤器()而不是的get()

这篇关于Django的返回单个纪录JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆