Hi Guys
I have some issue, by adding parts (Start roughly at 3:00) and if I add a product into the cart I get some failure as follow:
[29/May/2023 19:57:28] “GET /store/carts/f9532386-9ada-4638-a672-a206af143cd3/items/ HTTP/1.1” 200 18537
Internal Server Error: /store/carts/f9532386-9ada-4638-a672-a206af143cd3/items/
Traceback (most recent call last):
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/django/core/handlers/exception.py”, line 55, in inner
response = get_response(request)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/django/core/handlers/base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/django/views/decorators/csrf.py”, line 56, in wrapper_view
return view_func(*args, **kwargs)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/viewsets.py”, line 125, in view
return self.dispatch(request, *args, **kwargs)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/views.py”, line 509, in dispatch
response = self.handle_exception(exc)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/views.py”, line 469, in handle_exception
self.raise_uncaught_exception(exc)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/views.py”, line 480, in raise_uncaught_exception
raise exc
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/views.py”, line 506, in dispatch
response = handler(request, *args, **kwargs)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/mixins.py”, line 18, in create
serializer.is_valid(raise_exception=True)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/serializers.py”, line 227, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/serializers.py”, line 426, in run_validation
value = self.to_internal_value(data)
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-djBK3ZAE/lib/python3.9/site-packages/rest_framework/serializers.py”, line 485, in to_internal_value
validated_value = validate_method(validated_value)
File “/Users/rogerlooser/Documents/Privat/Schule/Python/storefront/store/serializers.py”, line 73, in validate_product_id
if not Product.objects.filter(pk=value).exist():
AttributeError: ‘QuerySet’ object has no attribute ‘exist’
[29/May/2023 19:57:35] “POST /store/carts/f9532386-9ada-4638-a672-a206af143cd3/items/ HTTP/1.1” 500 123109
If I take the line code where the mistake is shown please see below:
class AddCartItemSerializer(serializers.ModelSerializer):
product_id = serializers.IntegerField()
def validate_product_id(self, value):
if not Product.objects.filter(pk=value).exist():
raise serializers.ValidationError('No product with the given ID was found')
return value
Maybe somebody has some hints how I solve it.
Thanks
Roger