0

In my webapp, I have a friends feature, but one of the if statements produces an error

Here is my UserProfileInfo model

class UserProfileInfo(models.Model):
    connection = models.ManyToManyField(User,blank=True,related_name='follow_profile')

And now here is my view:

def friend_actions(request,username=None):
    current_user = request.user.userprofileinfo
    user = request.user
    # username = get("username")
    username = User.objects.get(username=username)
    other_user = get_object_or_404(UserProfileInfo,user__username=username)
    # other_user = UserProfileInfo.objects.get(username=username)
    url = other_user.get_absolute_url()
    if other_user in current_user.connection.all():
        current_user.connection.remove(other_user)
    else:
        current_user.connection.add(other_user)
    return HttpResponseRedirect(url)

However, this produces the following error:

argument of type 'QuerySet' is not iterable

Full traceback

Traceback:

File "C:\Users\User\.virtualenvs\interests-site-Ho6yLlHE\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\User\.virtualenvs\interests-site-Ho6yLlHE\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\User\.virtualenvs\interests-site-Ho6yLlHE\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\User\interests-site\interests-drf\mainapp\views.py" in friend_actions
  453.     if other_user in current_user.connection.all():

Exception Type: TypeError at /mainapp/profile/donnellan0007/connect/
Exception Value: argument of type 'QuerySet' is not iterable

I am wondering how I can stop this error from occurring. I have been stumped by it all day

1
  • 1
    Replace the in check with a query like current_user.connection.filter(<your condition>).exists() Commented Apr 3, 2020 at 9:46

1 Answer 1

1

This seems to be a bug showing up it's face time to time aka Heisenbug!

QuerySet indeed is an iterable (https://github.com/django/django/blob/master/django/db/models/query.py#L271)

Django's Forum has some info about this -

https://code.djangoproject.com/ticket/26600 https://github.com/PyCQA/pylint-django/issues/117

In General, the issue seems to be because of exception (other than StopIteration) being thrown inside the iter .

You can use one of the following routes -

  1. Force the Queryset to list (Not recommended)

  2. Filter on the connections using a condition. In your case search for other_user in the connection using filter

  3. Return values_list of values instead of Queryset and perform 'in' on that.

Sign up to request clarification or add additional context in comments.

1 Comment

.values_list() is still a QuerySet, which is suspectible to the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.