title: In Django how to mock an object method called by views.py during its import?
body: <p>I am writing System Tests for my Django app, where I test the complete application via HTTP requests and mock its external dependencies' APIs.</p>
  
  <p>In <em>views.py</em> I have something like:</p>
  
  <pre><code>from external_service import ExternalService
  externalService = ExternalService
  data = externalService.get_data()
  
  @crsf_exempt
  def endpoint(request):
      do_something()
  </code></pre>
  
  <p>What I want is to mock (or stub) ExternalService to return a predefined response when its method <code>get_data()</code> is called.</p>
  
  <p>The problem is that when I run <code>python manage.py test</code>, views.py is loaded before my test class. So when I patch the object with a mocked one, the function <code>get_data(</code>) was already called. </p>
  
  <p><a href="https://stackoverflow.com/a/8658332/8971507">This solution</a> didn't work either.</p>