Is it possible to override the create of a Viewset to first check if an object exists and, if so, return that object rather than creating it?
Specifically, in my viewset, I have overriden the create function as follows:
try:
item = Item.objects.get(recipe__id=self.request.data['recipe'])
except Item.DoesNotExist:
serializer.save(owner=self.request.user)
Any ideas how I can pass the item back? Currently this just spits back the input. Even if I serialize and return the item, this does not seem to work as below:
try:
item = Item.objects.get(
recipe__id=self.request.data['recipe'])
serializer = UserItemSerializer(item)
return Response(serializer.data, status=status.HTTP_201_CREATED)
except Item.DoesNotExist:
serializer.save(owner=self.request.user)