TypeError 'RelatedManager' object is not iterable

Raised by:

django/db/models/fields/related_lookups.py (line 47)

Traceback (most recent call last):
  File "python3.5/site-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "python3.5/site-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File "python3.5/site-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File "python3.5/site-packages/django/template/response.py", line 137, in rendered_content
    content = template.render(context, self._request)
  File "python3.5/site-packages/django/template/backends/django.py", line 95, in render
    return self.template.render(context)
  File "python3.5/site-packages/django/template/base.py", line 206, in render
    return self._render(context)
  File "python3.5/site-packages/django/template/base.py", line 197, in _render
    return self.nodelist.render(context)
  File "python3.5/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "python3.5/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "python3.5/site-packages/django/template/loader_tags.py", line 173, in render
    return compiled_parent._render(context)
  File "python3.5/site-packages/django/template/base.py", line 197, in _render
    return self.nodelist.render(context)
  File "python3.5/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "python3.5/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "python3.5/site-packages/django/template/loader_tags.py", line 69, in render
    result = block.nodelist.render(context)
  File "python3.5/site-packages/django/template/base.py", line 992, in render
    bit = node.render_annotated(context)
  File "python3.5/site-packages/django/template/base.py", line 959, in render_annotated
    return self.render(context)
  File "python3.5/site-packages/django/template/base.py", line 1043, in render
    output = self.filter_expression.resolve(context)
  File "python3.5/site-packages/django/template/base.py", line 709, in resolve
    obj = self.var.resolve(context)
  File "python3.5/site-packages/django/template/base.py", line 850, in resolve
    value = self._resolve_lookup(context)
  File "python3.5/site-packages/django/template/base.py", line 913, in _resolve_lookup
    current = current()
  File "private.py", line 1, in private_function
    private_function()
  File "python3.5/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "python3.5/site-packages/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "python3.5/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "python3.5/site-packages/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "python3.5/site-packages/django/db/models/sql/query.py", line 1263, in _add_q
    current_negated, allow_joins, split_subq)
  File "python3.5/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "python3.5/site-packages/django/db/models/sql/query.py", line 1199, in build_filter
    condition = lookup_class(lhs, value)
  File "python3.5/site-packages/django/db/models/lookups.py", line 19, in __init__
    self.rhs = self.get_prep_lookup()
  File "python3.5/site-packages/django/db/models/fields/related_lookups.py", line 47, in get_prep_lookup
    self.rhs = [get_normalized_value(val, self.lhs)[0] for val in self.rhs]
TypeError: 'RelatedManager' object is not iterable

How to reproduce

Pass a RelatedManager in a query that expects a an iterable.

class User(models.Model):
    name = models.CharField(max_length=255)


class Company(models.Model):
    name = models.CharField(max_length=255)
    owner = models.ForeignKey(User, related_name='companies_owned')


class Event(models):
    name = models.CharField(max_length=255)
    company = models.ForeignKey(Company)


Event.objects.filter(company__in=user.companies_owned)

In the example, user.companies_owned is a RelatedManager.

How to fix

Execute the RelatedManager query and pass it's results as arguments.

Event.objects.filter(company__in=user.companies_owned.all())