4 ms·
Neat. I guess I'm a little surprised that the Django REST Framework isn't mentioned, since I thought that's the go-to for pretty much everyone for this task. C
by acidburnNSA 4y ago
Neat.
I guess I'm a little surprised that the Django REST Framework isn't mentioned, since I thought that's the go-to for pretty much everyone for this task. Certainly this post's code is lighter weight if all you need to do is send some data out of Django.
https://www.django-rest-framework.org/ https://www.django-rest-framework.org/
- sdrm 4y agoDefinitely use DRF for this kind of usecases. The problem with his approach is that it'll always expose ALL the fields of a model to the frontend, like hashed passwords, and can come at a performance cost if the queryset was run with `.only("some", "fields")`. This can be tolerable for small projects but it doesn't scale too well on the long term...
- Ranebow6 4y agoSurely it will only expose the fields you've defined in your Serializer class? https://www.django-rest-framework.org/api-guide/serializers/#specifying-which-fields-to-include https://www.django-rest-framework.org/api-guide/serializers/...
- brento 4y agoIn the article, you'll notice I'm not using DRF.
- brento 4y agoI would certainly make improvements to the script to optimize the serialization of the QuerySet to only output the needed fields. It's hard to write an article that would capture every use case and optimization required.
- Chiron1991 4y agoDRF is for building REST APIs, but OP wants to dump the JSON-representation of an arbitrary object into an HTML template. That's a very different use case for which simple JSON-encoding and the json_script filter should suffice.