Home > Software engineering >  ValueError: Cannot query "sohanur": Must be "CustomUser" instance
ValueError: Cannot query "sohanur": Must be "CustomUser" instance

Time:02-26

I am trying to filter my vendor list as per request.user.vendor. It is working and get the requested user but here I can't query through my Vendor list. Vendor has a onetoone relation with my custom user. How to query??

#this is my model

class CustomUser(AbstractUser):
    number = models.CharField(max_length=200, blank=True, null=True)
    isVendor = models.BooleanField(default=False, blank=True, null=True)

class Vendor(models.Model):
    user = models.OneToOneField(CustomUser, related_name='vendor', on_delete=models.CASCADE, null=True, blank=True)
    distributor_for = models.ManyToManyField(Distributor, null=False, blank=True)
    name = models.CharField(max_length=200, blank=True, null=True)
    profile_picture = models.ImageField(null=True, blank=True)
    address = models.TextField(max_length=2000, blank=True, null=True)
    latitude = models.DecimalField(max_digits = 13, decimal_places = 7, blank=True, null=True)
    longitude = models.DecimalField(max_digits = 13, decimal_places = 7,blank=True, null=True)
    is_verified = models.BooleanField(default=False)
    createdAt = models.DateTimeField(auto_now_add=False, null=True, blank=True)

    def __str__(self):
        return self.name

#this is my api view

@api_view(['GET'])
@permission_classes([IsVendor])
def getVendors(request):
    user = request.user.vendor
    print(user)
    vendor = Vendor.objects.all().filter(user=user)
    serializer = VendorSerializer(vendor, many=False)
    return Response(serializer.data)

#this is my serializer

class VendorSerializer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Vendor
        fields = '__all__'

    def get_user(self, obj):
        user = obj.user
        serializer = UserSerializer(user, many=False)
        return serializer.data

##Error

Internal Server Error: /api/orders/vendors/
django_containe  | Traceback (most recent call last):
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
django_containe  |     response = get_response(request)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
django_containe  |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
django_containe  |     return view_func(*args, **kwargs)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
django_containe  |     return self.dispatch(request, *args, **kwargs)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
django_containe  |     response = self.handle_exception(exc)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
django_containe  |     self.raise_uncaught_exception(exc)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
django_containe  |     raise exc
django_containe  |   File "/usr/local/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
django_containe  |     response = handler(request, *args, **kwargs)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/rest_framework/decorators.py", line 50, in handler
django_containe  |     return func(*args, **kwargs)
django_containe  |   File "/django/base/views/order_views.py", line 252, in getVendors  
django_containe  |     vendor = Vendor.objects.all().filter(user=user)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 941, in filter
django_containe  |     return self._filter_or_exclude(False, args, kwargs)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 961, in _filter_or_exclude
django_containe  |     clone._filter_or_exclude_inplace(negate, args, kwargs)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 968, in _filter_or_exclude_inplace
django_containe  |     self._query.add_q(Q(*args, **kwargs))
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1393, in add_q
django_containe  |     clause, _ = self._add_q(q_object, self.used_aliases)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1412, in _add_q
django_containe  |     child_clause, needed_inner = self.build_filter(
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1320, in build_filter
django_containe  |     self.check_related_objects(join_info.final_field, value, join_info.opts)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1147, in check_related_objects
django_containe  |     self.check_query_object_type(value, opts, field)
django_containe  |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1128, in check_query_object_type
django_containe  |     raise ValueError(
django_containe  | ValueError: Cannot query "sohanur": Must be "CustomUser" instance.   
django_containe  | [25/Feb/2022 17:34:22] "GET /api/orders/vendors/ HTTP/1.1" 500 17281

check my traceback error and kindly help me to solve this.

CodePudding user response:

This doesn't look right, although not all the information is available in the question

user = request.user.vendor                       # a Vendor instance
vendor = Vendor.objects.all().filter(user=user)  # user field -> CustomUser
                                                 # =user, Vendor instance

Should it be .filter( user=request.user) ?

CodePudding user response:

change the query to,

vendor = Vendor.objects.filter(user=user)

in your API view.

  • Related