Best practice for managing magic strings in Django JsonResponse keys/values
I have a Django view that returns JSON responses:
def post(self, request, post_id):
post = get_object_or_404(Post, pk=post_id)
if post.is_pub_date_future():
return JsonResponse({
'result': 'failure', # magic string
'message': 'No posts available to dislike' # magic string
}, status=404)
post.dislikes = F("dislikes") + 1
post.save()
return JsonResponse({
'result': 'success', # magic string
'dislikes': post.dislikes, # magic string
})
The problem: I have to memorize all these string keys ('result', 'message', 'dislikes') and values ('success', 'failure'). If I change them in the view, I also have to update them in my tests and templates.
I'm considering creating a constants.py file like this
class ResultConstants:
ERROR = "error"
SUCCESS = "success"
FAILURE = "failure"
class EntityConstants:
DISLIKE = "dislike"
LIKE = "like"
But I'm not sure if this is the best approach
So what is the best practice for managing these magic strings in JSON responses?
And how exactly this problem is called?