Reputation: 289
I have set up a JsonResponse method in my views.py:
def get_rest_list(request):
if request.method == "GET":
image_list = Image.objects.order_by('-date')
serializer = ImageSerializer(image_list, many=True)
return JsonResponse(serializer.data, safe=False)
Now if I call that method with "http://localhost:8000/api/" I get a JSON from all Image objects that are in the db.
How can I get a specific object by its pk when I would do something like this: http://localhost:8000/api/1/ or maybe even: http://localhost:8000/api/445756/
Upvotes: 0
Views: 349
Reputation: 908
You can try something like:
def get_rest_item(request, image_id):
image_item = Image.objects.get(id=image_id)
serializer = ImageSerializer(image_item)
return JsonResponse(serializer.data, safe=False)
Upvotes: 1