title: How to check if an object has more than constant number of many to many field objects
body: <p>I have 2 tables say &quot;A&quot; and &quot;B&quot; having ManyToMany relationship with one of the field with table &quot;A&quot;, I have created a Model Admin from where I am creating both the tables object.</p>
  <p>I have a constraint that I have to create constant number of &quot;B&quot; object for each fields (relation)</p>
  <p>I have written below code but it gives an error:
  <code>ValueError: “needs to have a value for field ”id“ before this many-to-many relationship can be used</code></p>
  <pre><code>count = 10
  class A(models.Model):
      field_a = models.CharField(max_length=100)
  
  class B(models.Model):
      field_b = models.CharField(max_length=100)
      fields = models.ManyToManyField(A, related_name=&quot;bs&quot;)
  
      def save(*args, **kwargs):
          for item in self.fields.all():
              if item.bs.count() &gt; count:
                  raise ValidationError(
                      &quot;item count is exeeded&quot;
                  )
          return super(B, self).save(*args, **kwargs)
  </code></pre>
  <p>Please suggest how to handle this case.</p>