Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: TypeError - argument of type 'QuerySet' is not iterable

Tags:

python

django

So this error is very cryptic, how can it be that a Queryset object, even if empty, is not iterable?

This is the rare error I'm seeing:

TypeError: argument of type 'QuerySet' is not iterable

And this is the code producing this error:

artist_object = Artist.objects.get(id=id)
artist_release_groups = artist_object.release_groups.all()
if rg not in artist_release_groups: # this is the line where the error is  happening
    artist_object.release_groups.add(rg)

I don't know if this is relevant, but this is happening in a celery task and the error is being reported in Sentry (an exception reporting service).

Traceback that I have:

TypeError: argument of type 'QuerySet' is not iterable
  File "celery/app/trace.py", line 382, in trace_task
    R = retval = fun(*args, **kwargs)
  File "celery/app/trace.py", line 641, in __protected_call__
    return self.run(*args, **kwargs)
  File "core/tasks.py", line 234, in refresh_artist_task
    get_apple_release_groups_for_artist(applemusic_id)
  File "celery/local.py", line 191, in __call__
    return self._get_current_object()(*a, **kw)
  File "celery/app/trace.py", line 642, in __protected_call__
    return orig(self, *args, **kwargs)
  File "celery/app/task.py", line 375, in __call__
    return self.run(*args, **kwargs)
  File "core/tasks.py", line 147, in get_apple_release_groups_for_artist
    if rg not in artist_release_groups:

Update: not sure, but I think this related to this issue on the Django forums. (see last three replies)

like image 482
zerohedge Avatar asked Nov 08 '22 02:11

zerohedge


1 Answers

Maybe you should use a filter in the if. instead of: if rg not in artist_release_groups: artist_object.release_groups.add(rg)

use

artist_release_groups_query = artist_object.release_groups.filter(id=rg.id)
if not artist_rease_groups_query.exists():
    # add the rest
like image 178
ocobacho Avatar answered Nov 14 '22 23:11

ocobacho