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
incheck with a query likecurrent_user.connection.filter(<your condition>).exists()