Error in part 2 customizing generic views

i get an error after running this :

class ProductDetail(RetrieveUpdateDestroyAPIView):
queryset=Product.objects.all()
serializer_class=ProductSerializer
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)
product.delete()
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 models.py

2 Likes