[Answer]-Use of string in if() statement django error: string as left operand, not QuerySet

1👍

âś…

self.error_object holds the instance of QuerySet class. And no you can’t check if object of this type is inside the string.

QuerySet is a class which is a wrapper for Django ORM query/ies. It implements iterable protocol so you can iterate over it to get matching Model instances one by one.
Then you can access the fields of these instances as normal object attributes. If one of them is a string then you can check if it’s a substring of req.
It’s hard to say, what exactly you are trying to do but just a guess:

for model_instance in self.error_object:
    for req in request:
        if model_instance.some_string_field in req:
             print 'error Found in' + req

0👍

If you’re trying to check whether any of multiple objects’ string representations appear in your response (I am unable to figure out what other behavior “queryset in string” might be intended to create) you want something like:

error_strings = [str(val) for val in self.error_object]
...
# then in your loop
if any(val in req for val in error_strings):

You might also profile creating an ord together regexp of error strings.

Leave a comment