I use Django Camel Case to swap from pythons snake_case in the backend to camelCase in the front end, so the keys of the data comply with conventional JavaScript syntax.
This works great for AJAX calls returning JSON.
However often I wish to embed some JSON in an HTML template with Django's standard 'json_script`. I don't wish to use some other JSON strinfier because then it won't be safely escaped.
The problem is that when I embedded the JSON in HTML, it is not camelCased:
Example
# --- models.py ---
from django.db import models
class Foo(models.Model):
last_name = models.CharField(max_length=250, unique=True)
# --- serializers.py ---
from rest_framework import serializers
class FooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
fields = ('id', 'last_name')
# --- views.py ---
from django.views import generic
from . import models
class IndexView(generic.ListView):
template_name = f"myapp/index.html"
model = Foo
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['serializer'] = serializers.FooSerializer(self.object_list, many=True)
return context
Then in the template:
{{ serializer.data|json_script:"FOO_DATA" }}
<script type="module">
const foos = JSON.parse(document.getElementById('FOO_DATA').textContent);
console.log(foos)
</script>
But unfortunately it will result in something like:
[{ id: 1, last_name: 'Smith'}, { id: 2, last_name: 'Kent'}]
But I require last_name
to be camel cased:
[{ id: 1, lastName: 'Smith'}, { id: 2, lastName: 'Kent'}]
How can one safely embedded JSON data, and convert it to camelCase?