payload: {"action":"created","issue":{"url":"https://api.github.com/repos/encode/django-rest-framework/issues/5403","repository_url":"https://api.github.com/repos/encode/django-rest-framework","labels_url":"https://api.github.com/repos/encode/django-rest-framework/issues/5403/labels{/name}","comments_url":"https://api.github.com/repos/encode/django-rest-framework/issues/5403/comments","events_url":"https://api.github.com/repos/encode/django-rest-framework/issues/5403/events","html_url":"https://github.com/encode/django-rest-framework/issues/5403","id":256402770,"node_id":"MDU6SXNzdWUyNTY0MDI3NzA=","number":5403,"title":"Many to Many field with through models not nesting properly.","user":{"login":"neighlyd","id":9094023,"node_id":"MDQ6VXNlcjkwOTQwMjM=","avatar_url":"https://avatars0.githubusercontent.com/u/9094023?v=4","gravatar_id":"","url":"https://api.github.com/users/neighlyd","html_url":"https://github.com/neighlyd","followers_url":"https://api.github.com/users/neighlyd/followers","following_url":"https://api.github.com/users/neighlyd/following{/other_user}","gists_url":"https://api.github.com/users/neighlyd/gists{/gist_id}","starred_url":"https://api.github.com/users/neighlyd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/neighlyd/subscriptions","organizations_url":"https://api.github.com/users/neighlyd/orgs","repos_url":"https://api.github.com/users/neighlyd/repos","events_url":"https://api.github.com/users/neighlyd/events{/privacy}","received_events_url":"https://api.github.com/users/neighlyd/received_events","type":"User","site_admin":false},"labels":[{"id":250491223,"node_id":"MDU6TGFiZWwyNTA0OTEyMjM=","url":"https://api.github.com/repos/encode/django-rest-framework/labels/Usage","name":"Usage","color":"dddddd","default":false,"description":null}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-09-09T00:46:57Z","updated_at":"2019-11-28T08:27:05Z","closed_at":"2017-09-09T15:37:58Z","author_association":"NONE","body":"When attempting to serialize a Many to Many field with through models, an error is thrown that the final model object does not have the appropriate attribute.\r\n\r\n## Steps to reproduce\r\n\r\n```python\r\n# models.py\r\nclass Person(models.Model):\r\n\r\n    name = models.CharField(max_length=50)\r\n\r\n    def __str__(self):\r\n        return self.name\r\n\r\n\r\nclass Case(models.Model):\r\n\r\n    summary = models.TextField()\r\n    litigant_count = models.ManyToManyField(Person, through='Litigant')\r\n\r\n    def __str__(self):\r\n        return 'Case %s' % (self.id)\r\n\r\nclass Litigant(models.Model):\r\n\r\n    person = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='person_to_case')\r\n    case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name='case_to_person')\r\n    role = models.TextField()\r\n```\r\n\r\n```python\r\n# serializers.py\r\nclass PersonSerializer(ModelSerializer):\r\n\r\n    class Meta:\r\n        model = Person\r\n        fields = ('name')\r\n\r\n\r\nclass LitigantSerializer(ModelSerializer):\r\n\r\n    class Meta:\r\n        model = Litigant\r\n        fields = ('person', 'case', 'role')\r\n\r\n\r\nclass CaseSerializer(ModelSerializer):\r\n\r\n    queryset = Litigant.objects.all()\r\n    litigant_count = LitigantSerializer(queryset, many=True, read_only=True)\r\n\r\n    class Meta:\r\n        model = Case\r\n        fields = ('summary', 'litigant_count')\r\n```\r\n\r\n```python\r\n# views.py\r\n\r\nclass PersonViewSet(ModelViewSet):\r\n    queryset = Person.objects.all()\r\n    serializer_class = PersonSerializer\r\n\r\n\r\nclass LitigantViewSet(ModelViewSet):\r\n    queryset = Litigant.objects.all()\r\n    serializer_class = LitigantSerializer\r\n\r\n\r\nclass CaseViewSet(ModelViewSet):\r\n    queryset = Case.objects.all()\r\n    serializer_class = CaseSerializer\r\n```\r\n\r\n\r\n## Expected behavior\r\n\r\nI expected a nested serializer like that shown in the documents under [Nested Relationships](http://www.django-rest-framework.org/api-guide/relations/#nested-relationships), as modified by the [Many to Many with Through Model section](http://www.django-rest-framework.org/api-guide/relations/#manytomanyfields-with-a-through-model) indicates.\r\n\r\nAfter searching extensively on the web (Stackoverflow, Reddit, github, and the Google Group), all answers appear to indicate that including `many=True` should cause the serializers to work ([see, for example, this thread](https://stackoverflow.com/questions/33182092/django-rest-framework-serializing-many-to-many-field)). [Another thread](https://stackoverflow.com/questions/14032783/how-to-work-with-manytomanyfield-with-through-relation-in-django-rest-framework) indicated that the fields in the intermediate model needed to be set explicitly to `ReadOnlyField`, as though `read_only=True` was not adequate on its own. Neither of these resolves the issue (though the latter does display the API, it simply truncates the through model entirely from the serialization).\r\n\r\n## Actual behavior\r\n\r\nWhen accessing the `Cases` API, it responds with the following error:\r\n\r\n```\r\n'Person' object has no attribute 'person'\r\n```\r\n\r\nThe stack trace is as follows:\r\n\r\n```\r\nTraceback:\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/django/core/handlers/exception.py\" in inner\r\n  41.             response = get_response(request)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/django/core/handlers/base.py\" in _get_response\r\n  187.                 response = self.process_exception_by_middleware(e, request)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/django/core/handlers/base.py\" in _get_response\r\n  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/django/views/decorators/csrf.py\" in wrapped_view\r\n  58.         return view_func(*args, **kwargs)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/viewsets.py\" in view\r\n  90.             return self.dispatch(request, *args, **kwargs)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/views.py\" in dispatch\r\n  489.             response = self.handle_exception(exc)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/views.py\" in handle_exception\r\n  449.             self.raise_uncaught_exception(exc)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/views.py\" in dispatch\r\n  486.             response = handler(request, *args, **kwargs)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/mixins.py\" in list\r\n  48.         return Response(serializer.data)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in data\r\n  739.         ret = super(ListSerializer, self).data\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in data\r\n  263.                 self._data = self.to_representation(self.instance)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in to_representation\r\n  657.             self.child.to_representation(item) for item in iterable\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in <listcomp>\r\n  657.             self.child.to_representation(item) for item in iterable\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in to_representation\r\n  501.                 ret[field.field_name] = field.to_representation(attribute)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in to_representation\r\n  657.             self.child.to_representation(item) for item in iterable\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in <listcomp>\r\n  657.             self.child.to_representation(item) for item in iterable\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/serializers.py\" in to_representation\r\n  488.                 attribute = field.get_attribute(instance)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/relations.py\" in get_attribute\r\n  179.         return get_attribute(instance, self.source_attrs)\r\n\r\nFile \"/home/vagrant/.virtualenvs/testsite/lib/python3.4/site-packages/rest_framework/fields.py\" in get_attribute\r\n  103.                 instance = getattr(instance, attr)\r\n\r\nException Type: AttributeError at /api/cases/\r\nException Value: 'Person' object has no attribute 'person'\r\n```\r\n\r\n## Checklist\r\n\r\n- [√] I have verified that that issue exists against the `master` branch of Django REST framework.\r\n- [√] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.\r\n- [√] This is not a usage question. (Those should be directed to the [discussion group](https://groups.google.com/forum/#!forum/django-rest-framework) instead.)\r\n- [√] This cannot be dealt with as a third party library. (We prefer new functionality to be [in the form of third party libraries](http://www.django-rest-framework.org/topics/third-party-resources/#about-third-party-packages) where possible.)\r\n- [√] I have reduced the issue to the simplest possible case.\r\n- [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/encode/django-rest-framework/issues/comments/559390854","html_url":"https://github.com/encode/django-rest-framework/issues/5403#issuecomment-559390854","issue_url":"https://api.github.com/repos/encode/django-rest-framework/issues/5403","id":559390854,"node_id":"MDEyOklzc3VlQ29tbWVudDU1OTM5MDg1NA==","user":{"login":"Allan-Nava","id":22498435,"node_id":"MDQ6VXNlcjIyNDk4NDM1","avatar_url":"https://avatars3.githubusercontent.com/u/22498435?v=4","gravatar_id":"","url":"https://api.github.com/users/Allan-Nava","html_url":"https://github.com/Allan-Nava","followers_url":"https://api.github.com/users/Allan-Nava/followers","following_url":"https://api.github.com/users/Allan-Nava/following{/other_user}","gists_url":"https://api.github.com/users/Allan-Nava/gists{/gist_id}","starred_url":"https://api.github.com/users/Allan-Nava/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Allan-Nava/subscriptions","organizations_url":"https://api.github.com/users/Allan-Nava/orgs","repos_url":"https://api.github.com/users/Allan-Nava/repos","events_url":"https://api.github.com/users/Allan-Nava/events{/privacy}","received_events_url":"https://api.github.com/users/Allan-Nava/received_events","type":"User","site_admin":false},"created_at":"2019-11-28T08:27:05Z","updated_at":"2019-11-28T08:27:05Z","author_association":"NONE","body":"How can I create the manyToMany fields like the example ? i have the problem that is not possibile to create with drf"}}
other: {"actor":{"display_login":"Allan-Nava"}}