Error in part 2 customizing generic views

i get an error after running this :

class ProductDetail(RetrieveUpdateDestroyAPIView):
def delete(self,request,pk):
product = get_object_or_404(Product, pk=pk)
if product.orderitems.count() > 0:
return Response({‘error’: ‘Product cannot be deleted because it is associated with an order item.’}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
return Response(status=status.HTTP_204_NO_CONTENT)

the error message is :
‘Product’ object has no attribute ‘orderitems’

please make sure you included related_name=‘orderitems’ in the product foreign key variable in

1 Like