title: Django test: TransactionManagementError: You can't execute queries until the end of the 'atomic' block
body: <p>Django newbie here. I'm trying to implement unit tests for a simple API I developed. Below you can find my test implementation which works fine:</p>
  
  <pre><code>from django.test import TestCase
  from my_app.models import MyModel
  
  
  class TestMyViewSet(TestCase):
      """
      Unit tests for endpoints in MyViewSet.
      """
      fixtures = ['my_app/fixtures/data.yaml']
  
      def setUp(self):
          # Making setup for the test case here.
  
  
      def test_post_my_resource(self):
  
          # Checking that fixture is loaded correctly.
          self.assertEqual(MyModel.objects.all().count(),1)
  
          request_body = {
              'my_resource': "input_for_my_resource"
          }
  
          response = self.client.post('/my_resources/', data=request_body)
          self.assertEqual(response.status_code, 201)
          # self.assertEqual(MyModel.objects.all().count(),2)
  </code></pre>
  
  <p>But when I removed the last line <code>self.assertEqual(MyModel.objects.all().count(),2)</code> from the comment to test that <code>my_resource</code> is created successfully on the corresponding model by checking the number of instances, I got an error stating the following:</p>
  
  <blockquote>
    <p>TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.</p>
  </blockquote>
  
  <p>What am I missing here? </p>
  
  <p>Thanks in advance!</p>
  
  <p>PS: I come across the following question: <a href="https://stackoverflow.com/questions/21458387/transactionmanagementerror-you-cant-execute-queries-until-the-end-of-the-atom">TransactionManagementError “You can't execute queries until the end of the 'atomic' block” while using signals, but only during Unit Testing</a> but I'm not sure what's happening in my case is the same.</p>