title: Setting field values for M2M object in djnago
body: <p>I have two models in my application and I am trying to update a M2M field . Django documentation says that it can be done using set() method . But I guess by dealt set() accepts pk of the objects as arguments  and what I want to do is set it using the actual values .</p>
  <p>For e.g. I have a client in Clients models named &quot;adam&quot; and I want to set the value for its corresponding M2M field &quot;items_onsale&quot; to [&quot;ac&quot; , &quot;book&quot;]</p>
  <p>Below is my code for the models :-</p>
  <pre><code>from django.db import models
  
  
  class Client(models.Model):
      SEX_CHOICES = [('M', 'Male'), ('F', 'Female')]
      fname = models.CharField(max_length=100)
      lname = models.CharField(max_length=100)
      mailid = models.EmailField(max_length=100)
      sex = models.CharField(max_length=1, choices=SEX_CHOICES, blank=True)
      age = models.IntegerField()
      items_onsale = models.ManyToManyField('Sinfo', blank=True)
  
      def __str__(self):  # for displaying the variable correctly.
          return self.fname , self.lname , self.mailid , self.sex , self.age , self.items_onsale
  
  
  class Sinfo(models.Model):  # data of items put on sale by clients
      iname = models.CharField(max_length=100)
      idesc = models.TextField(max_length=300, null=True)
  
      def __str__(self):  # for displaying the variable correctly.
          return self.iname
  
  
  </code></pre>
  <p>What I have tried till now is :-</p>
  <pre><code>
  c = Client.objects.get(pk=17)
  list=[&quot;ac&quot;,&quot;book&quot;]
  c.items_onsale_set(list)
  
  </code></pre>
  <p>And I am getting below error :-</p>
  <p><strong>ValueError: Field 'id' expected a number but got 'book'</strong></p>
  <p>I know that there is a way to update it using values but not sure how . The django documentation does suggest using &quot;through_defaults&quot; option but haven't given any such example , so I am quite not sure how to use it  :-</p>
  <p>For many-to-many relationships set() accepts a list of either model instances or field values, normally primary keys, as the objs argument.</p>
  <p>Use the through_defaults argument to specify values for the new intermediate model instance(s), if needed. You can use callables as values in the through_defaults dictionary and they will be evaluated once before creating any intermediate instance(s).</p>
  <p>I am there is a better to achieve this , please let me know .</p>