title: TransactionManagementError While Executing Unittest in Django Rest Framework
body: <p>I have written a test which checks whether <em>IntegrityError</em> was raised in case of duplicate records in the database. To create that scenario I am issue a REST API twice. The code looks like this:</p>
  
  <pre><code>class TestPost(APITestCase):
      @classmethod
      def setUpClass(cls):
          super().setUpClass()
          common.add_users()
  
      def tearDown(self):
          super().tearDown()
          self.client.logout()
  
      def test_duplicate_record(self):
          # first time
          response = self.client.post('/api/v1/trees/', dict(alias="some name", path="some path"))
          # same request second time
          response = self.client.post('/api/v1/trees/', dict(alias="some name", path="some path"))
          self.assertEqual(response.status_code, status.HTTP_400_BAD_RREQUEST)
  </code></pre>
  
  <p>But I get an error stack like this</p>
  
  <pre><code> "An error occurred in the current transaction. You can't "
  django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
  </code></pre>
  
  <blockquote>
    <p>How can I avoid this error this is certainly undesirable.</p>
  </blockquote>