title: Django CreateView test fail
body: <p>My configuration below (django 3.02):</p>
  
  <p>Models:</p>
  
  <pre><code>class Assets(models.Model):
      assettag = models.CharField()
      ...
  
  class Item(models.Model):
      asset = models.OneToOneField('Assets')
      ...
  
      def __str__(self):
          return (f"{self.rfid_tag}" + " - " + f"{self.asset.assettag}")
  
  class Comments(models.Model): # I know, it should not be plural.
      item = models.ForeignKey('Item',
                               default=None,
                               null=True,
                               on_delete=models.CASCADE,
                               )
      text = models.TextField()
      ...
  </code></pre>
  
  <p>Form:</p>
  
  <pre><code>class ItemInsertForm(forms.ModelForm):
      rfid_tag = forms.CharField(label='RFID Tag',
                                 widget=forms.TextInput(attrs={"placeholder":"96 bit EPC code",
                                                               "pattern":"^[a-fA-F0-9]{24}$",
                                                               "size":"25",
                                                               "id":"rfid_tag",
                                                               }
                                                        )
                                 )
      asset = forms.CharField(label='AssetTag',
                              widget=forms.TextInput(attrs={"placeholder":"Item Inventory Tag",
                                                            "pattern":"{some regex}",
                                                            "size":"25",
                                                            "id":"asset",
                                                            }
                                                     )
                              )
  
      comments = forms.CharField(label='Comments',
                                 required=False,
                                 widget=forms.Textarea(attrs={"rows":5,
                                                              "cols":50,
                                                              "id":"comments",
                                                             }
                                                      )
                                )
  
      class Meta:
          model = Item
          fields = [
              'rfid_tag',
              'asset',
              'image',
              'date',
          ]
  
  
      def clean_rfid_tag(self, *args, **kwargs):
          return self.cleaned_data.get("rfid_tag").lower()
  
      def clean_asset(self, *args, **kwargs):
          AssetTag = self.cleaned_data.get("asset")
          try:
              _asset = Assets.objects.get(assettag = AssetTag)
          except Assets.DoesNotExist:
              raise forms.ValidationError("Asset does not exist !")
          self.Asset = _asset
          return self.Asset
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>class InsertView(SuccessMessageMixin, AuditMixin, generic.CreateView):
      template_name = '{some template}'
      success_message = "Item succesfully created"
      form_class = ItemInsertForm
  
      def form_valid(self, form):
          item = form.save(commit=False)
          _comment = form.cleaned_data.get('comments')
          item = form.save()
          if _comment:
              item.comments_set.create(text =_comment)
          self.log_insert(item)
          return super().form_valid(form)
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>class TestViews(TestCase):
      @classmethod
      def setUpTestData(cls) -&gt; None:
          # create dummy assets
          cls.asset = Assets.objects.create(assettag="{some assettag #1}",)
          cls.asset2 = Assets.objects.create(assettag="{some assettag #2}")
          # create dummy item
          cls.item = Item.objects.create(rfid_tag='abcdef012345678900000000', asset=cls.asset)
  
      def setUp(self) -&gt; None:
          self.client = Client()
          self.insert_url = reverse('inventory:insert')
  
      def test_insert_POST_valid(self):
          response = self.client.post(self.insert_url,
                                      data = {'rfid_tag':'abcdef012345678900000001',
                                              'asset':'{some assettag #2}',}
                                      )
          new_item = Item.objects.get(rfid_tag="abcdef012345678900000001")
          self.assertEqual(response.status_code, 302)
          self.assertTrue(isinstance(new_item,Item))
          self.assertEqual(Item.objects.filter(rfid_tag="abcdef012345678900000001").count(),1)
          self.assertEqual(self.asset2.item, new_item)
          self.assertFalse(Comments.objects.all())
  
      def test_insert_POST_comment(self):
          response = self.client.post(self.insert_url,
                                      data = {'rfid_tag':'abcdef012345678900000001',
                                              'asset':'{some assettag #2}',
                                              'comments':'Test comment.',
                                              }
                                      )
          new_item = Item.objects.get(rfid_tag="abcdef012345678900000001")
          self.assertEqual(response.status_code, 302)
          self.assertTrue(isinstance(new_item,Item))
          self.assertEqual(Item.objects.filter(rfid_tag="abcdef012345678900000001").count(),1)
          self.assertEqual(self.asset2.item, new_item)
          self.assertTrue(isinstance(Comments.objects.get(text='Test comment.'), Comments))
          self.assertEqual(Comments.objects.get(text='Test comment.'),
                           new_item.comments_set.first())
  </code></pre>
  
  <p>If I run these tests, <code>test_insert_POST_valid</code> will fail due to assertion error:</p>
  
  <blockquote>
    <p>AssertionError: &lt; Item: abcdef012345678900000001 - {some assettag #2} >
    != &lt; Item: abcdef012345678900000001 - {some assettag #2} ></p>
  </blockquote>
  
  <p>I have inserted some print statements throughout the code:</p>
  
  <pre><code>    # this was placed first in both tests, before the POST request
      items = Item.objects.all()
      for item in items:
          print("Item:",item.id, " - ", item)
  
      # and then again, after each POST request
  </code></pre>
  
  <p>What I noticed is that the first time the prints run, it will output just one <code>Item</code> object (this is true for both functions):</p>
  
  <blockquote>
    <p>Item: 1  -  abcdef012345678900000009 - {some assettag #1}</p>
  </blockquote>
  
  <p>but on the second run (after the POST request is sent and the new item is created):</p>
  
  <p>For <code>test_insert_POST_comment</code> the result is:</p>
  
  <blockquote>
    <p>Item: 1  -  abcdef012345678900000009 - {some assettag #1} <br>
    Item: 2  - abcdef012345678900000001 - {some assettag #2}</p>
  </blockquote>
  
  <p>While for <code>test_insert_POST_valid</code> the result is:</p>
  
  <blockquote>
    <p>Item: 1  -  abcdef012345678900000009 - {some assettag #1} <br>
    Item: 3  - abcdef012345678900000001 - {some assettag #2}</p>
  </blockquote>
  
  <p>If I do <code>print("Asset Item:", self.asset2.item.id, " - ", self.asset2.item)</code> in the <code>test_insert_POST_valid</code> function (for some reason this test runs second) it will output:</p>
  
  <blockquote>
    <p>Asset Item: 2 - abcdef012345678900000001 - {some assettag #2}</p>
  </blockquote>
  
  <p>I don't understand how this happens. <code>Item.objects.all()</code> will only return one object (abcdef012345678900000000), while the <code>Asset</code> object still maintains a relationship with a fictive second <code>Item</code> object, that does not exist.</p>
  
  <p>I was suspecting that <code>test_insert_POST_valid</code> is not actually creating any object, since <code>self.asset2</code> already has an <code>Item</code> associated throug a <code>OneToOne</code> relationship, but this is not true. If I change the <code>rfid_tag</code> value to abcdef012345678900000002 (or whatever), it will create the new <code>Item</code>, but <code>self.asset2.item</code> will still point to abcdef012345678900000001.</p>
  
  <p>What is the cause of this issue and wow can I solve it? (Putting self.asset2.item = None before POST-ing the data, will not help. A new relationship will not be created.)</p>
  
  <p>Edit:
  I could move <code>cls.asset2 = Assets.objects.create(assettag="{some assettag #2}")</code> in the <code>setUp</code> method and it will be created before each test, but I have some more logic depending on this I would have to move it too. The prefered way is to leave it in <code>setUpTestdata</code> so it runs only once.</p>