Reputation: 109
I have a simple class :
class Item(models.Model):
name = models.CharField(max_length=250)
I need to serialize it to JSON with the ID as part of the JSON, but the ID never gets serialized when called in the view:
items_json = serializers.serialize('json', item_list)
The item_list json as received by the browser contains only name, not the ID. I need the ID for the processing on the client side which returns to the server later.
1) I'd like to avoid creating another ID if possible
2) I haven't created a Serializer Class. I am not sure where and how to do that.
Upvotes: 2
Views: 997
Reputation: 109
I found the solution. The pk field that is sent over is the id. The id will now show up in the fields. So in javascript you should do:
$.each( $.parseJSON(data), function(key, val) {
console.log("ID: " + val.pk)
});
Upvotes: 1