2

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.

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')

The first 2 each produce:

'Note' object is not iterable

And the 3rd one gave me:

<Note: Note object> is not JSON serializable

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.

Thanks.

2 Answers 2

10

From the docs:

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

Use filter() instead of get().

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I read the docs and saw that it had to be a query set, I didn't realize that it would work with .filter().
I really think neoprolog's answer is exactly what you are looking for in this case. Not everyone has the query set they want to use at the moment they want to serialize.
10

Use:

serializers.serialize("json", [new_note])

(attention to square brackets around query object)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.