request with list of id's for many to many relationship in django rest framework
<p>What is the correct way of implementing the following api with django and django-rest-framework. First the model</p>

<p><strong>Model</strong></p>

<pre><code>def ChatModel(models.Model):
   users = models.ManyToManyField(User, through=ChatUser)
</code></pre>

<p><strong>Response</strong></p>

<p>The desired response when getting this model where the many to many relationship is presented with a nested serializer:</p>

<pre><code>[
  {
    "id": 1,
    "title": "gossip",
    "users": [
        {
          "id": 1,
          "name": "George"
        },
        {
          "id": 2,
          "name": "Jerry"
        }  
    ]         
  }
]
</code></pre>

<p>The request is the critical part: Have the api receive a list of primary keys and create the user -> chat relationship in the backend.</p>

<p><strong>Request</strong></p>

<pre><code>{
  "title": "gossip",
  "users": [1,2]
}
</code></pre>

<p>So how do I allow for such a request to create an object with a many to many relationship.</p>

<p><strong>Edit</strong></p>

<p>I've been trying to implement this using a <code>ChatRequestSerializer</code> class that makes users field something like this </p>

<pre><code> users = serializers.ListField(child=serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all()))
</code></pre>

<p>and handle creating the relationships by overriding the <code>create</code> method of the serializer. This doesn't work because it throws the error: 'ManyRelatedManager' object is not iterable</p>