This might have been discussed in the past, apologies if it’s the case, I couldn’t find the discussion in the forum.
I have many projects that are not full blown SPA and don’t really require all the bells and whistles of DRF. However, it’s quite complex to write even the most simple APIs without another framework on top of Django. I think it would be awesome if Django offered an easy way to do simple APIs out of the box.
The way I see it, it could be a stripped down versions of generic CBVs that output JSON like this :
class JsonListView(View):
model = None
fields = None
paginate_by = 25
page_kwarg = "page"
def get_queryset(self):
return self.model.objects.all()
def get_paginated_queryset(self, queryset,):
page = self.request.GET.get(self.page_kwarg) or 0
start = int(page) * self.paginate_by
end = start + self.paginate_by
return queryset[start:end]
def get(self, request):
queryset = self.get_queryset().values(*self.fields)
queryset = self.get_paginated_queryset(queryset)
return JsonResponse(list(queryset), safe=False)
class UserListView(JsonListView):
fields = ["id", "title", "body"]
model = User
The benefits:
- Very easy to learn if you already use CBV
- Quick to draft simple APIs without having to add and learn new libraries
- Makes vanilla Django easier to integrate with popular javascript frontends
If the idea sounds nice, I would like to volunteer to make a formal proposal with the complete API for the views.