Django的返回单个纪录JSON纪录、Django、JSON

2023-09-10 18:01:06 作者:做自己的英雄

我添加有关我们的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()