We created custom JsonResponse status subclasses for a project and have been using them regularly ever since. So I thought I would see if there would an interest for something like this among the Django developer community. We have been using the JsonResponse class frequently lately, and the subclasses have really helped us with keeping our code readable and the JSON responses more consistent.
I have it set up similar to the HttpResponse subclasses, only the custom classes take a specific message and any additional arguments we want to add in.
I didn’t see any proposals on here or in the Google group. Do you think there is a need for these? If so, I’ll create a ticket with Django. Suggestions are welcome!
Just personally, I wouldn’t add these subclasses myself… They’re very lightweight, and easy to add in user projects if so desired.
I know we have the Http versions already, but I’ve often thought that they’re unnecessary: it takes longer to pull up the docs to find the right subclass then it does to pass the status code to the response constructor. On the flip side — going the other way — it takes longer to map class names back to the status code than it would do to just read it.
Maybe some folks find the class names more memorable IDK. I know for me the status codes themselves (or the HTTPStatus constants) are more useful to see and use.
Member of the same team here. We found that the proposed subclasses are easier to read and use consistently, and felt that the pattern is in line with the existing (and maybe overengineered, sure) HttpResponse subclasses. If we were submitting a PR for this, I would probably align to the existing HttpResponse subclasses for the most common use cases: if somebody needs 418, for example, they can go the custom route.
At any rate, the community has spoken! We’ll probably fold the code into a local Django helper library and if anybody comes along and is interested in this, feel free to reach out.
That’d make a good blog post! “Here are the N JsonResponse subclasses we needed in our project”, and some source. Others could copy and adapt as needed.