Looking to retrieve model data as an object in Javascript, I've been using the following approach in my Django view:
var data = {{ data|safe }};
Here is how my view is set up:
context = {
'data': {
'model1': serializers.serialize('json', model1.objects.all()),
'model2': serializers.serialize('json', model2.objects.all()),
}
};
The issues I'm encountering are:
1) An error occurs in JS unless I apply "safe" to the context variable,
2) Even with "safe", the object is returned as a string rather than a usable object (e.g. accessing data.model1[0] yields "[" instead of the first element).
Seeking guidance on the correct way to achieve this functionality.