Reputation: 479
I have such a scenario where I want to delete the saved model through its serializer:
class SomeView(APIView):
...
def post(self, request, context):
serializer = MySerializer(data = request.data)
# I need to save the model since I need the file field in the following processings
if serializer.is_valid():
serializer.save()
try:
...
except:
# I need to delete the model in case of exception
serializer.delete()
But there seems not to exist a way to delete the saved model through its serializer because I got this error AttributeError: 'VideoSerializer' object has no attribute 'delete'
Upvotes: 1
Views: 3507
Reputation: 3424
The serializer.save()
returns the saved object.
instance = None
if serializer.is_valid():
instance = serializer.save()
try:
...
except:
instance.delete()
That should delete the object being saved.
Upvotes: 1