title: In FactoryBoy, how do I setup my factory with an empty many-to-many member field?
body: <p>I'm using Django 3 with Python 3.8.  I have the following model ...</p>
  <pre><code>class Coop(models.Model):
      objects = CoopManager()
      name = models.CharField(max_length=250, null=False)
      types = models.ManyToManyField(CoopType, blank=False)
      addresses = models.ManyToManyField(Address)
      enabled = models.BooleanField(default=True, null=False)
      phone = models.ForeignKey(ContactMethod, on_delete=models.CASCADE, null=True, related_name='contact_phone')
      email = models.ForeignKey(ContactMethod, on_delete=models.CASCADE, null=True, related_name='contact_email')
      web_site = models.TextField()
  </code></pre>
  <p>I have created the following factory (using Factory boy) to attempt create the model in a test ...</p>
  <pre><code>class CoopFactory(factory.DjangoModelFactory):
      &quot;&quot;&quot;
          Define Coop Factory
      &quot;&quot;&quot;
      class Meta:
          model = Coop
  
      name = &quot;test model&quot;
      enabled = True
      phone = factory.SubFactory(PhoneContactMethodFactory)
      email = factory.SubFactory(EmailContactMethodFactory)
      web_site = &quot;http://www.hello.com&quot;
  
      @factory.post_generation
      def addresses(self, create, extracted, **kwargs):
          if not create:
              # Simple build, do nothing.
              return
  
          if extracted:
              # A list of types were passed in, use them
              for address in extracted:
                  self.addresses.add(address)
          else:
              address = AddressFactory()
              self.addresses.add( address )
  
      @factory.post_generation
      def types(self, create, extracted, **kwargs):
          if not create:
              # Simple build, do nothing.
              return
  
          if extracted:
              # A list of types were passed in, use them
              for type in extracted:
                  self.types.add(type)
          else:
              print(&quot;Creating type ...\n&quot;)
              type = CoopTypeFactory()
              self.types.add( type )
  </code></pre>
  <p>but I'm having trouble creating a factory with a many-to-many field (types) that is empty.  I tried the below</p>
  <pre><code>@pytest.mark.django_db
  def test_coop_create_with_no_types(self):
      &quot;&quot;&quot; Test customer model &quot;&quot;&quot;    # create customer model instance
      coop = CoopFactory.create(types=[])
      print(&quot;size: &quot;, coop.types.all().count())
      self.assertIsNotNone(coop)
      self.assertIsNotNone( coop.id )
  </code></pre>
  <p>but the value of <code>types.all().count()</code> is always equal to 1.  How do I properly setup a factory with an empty many-to-many field?</p>
  <p><b>Edit:</b> In response to the answer given, what's the right way to pass in the member field to be used by the factory?  I tried</p>
  <pre><code>@pytest.mark.django_db
  def test_coop_create_with_existing_type(self):
      &quot;&quot;&quot; Test customer model &quot;&quot;&quot;    # create customer model instance
      coop_from_factory = CoopFactory()
      self.assertIsNotNone(coop_from_factory)
  
      coop_types = coop_from_factory.types
      coop = CoopFactory.create(types=[coop_types.all().first()], addresses=coop_from_factory.addresses.all())
      self.assertIsNotNone(coop)
  </code></pre>
  <p>but got this error, for the line &quot;for _ in range(extracted):&quot; line ...</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/Users/davea/Documents/workspace/chicommons/maps/web/tests/test_models.py&quot;, line 48, in test_coop_create_with_existing_type
      coop = CoopFactory.create(types=coop_types, addresses=coop_from_factory.addresses.all())
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/factory/base.py&quot;, line 564, in create
      return cls._generate(enums.CREATE_STRATEGY, kwargs)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/factory/django.py&quot;, line 141, in _generate
      return super(DjangoModelFactory, cls)._generate(strategy, params)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/factory/base.py&quot;, line 501, in _generate
      return step.build()
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/factory/builder.py&quot;, line 296, in build
      postgen_results[declaration_name] = declaration.declaration.call(
    File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/factory/declarations.py&quot;, line 622, in call
      return self.function(
    File &quot;/Users/davea/Documents/workspace/chicommons/maps/web/tests/factories.py&quot;, line 128, in types
      for _ in range(extracted):
  TypeError: 'ManyRelatedManager' object cannot be interpreted as an integer
  </code></pre>