title: How to mock a Django model object (along with its methods)?
body: <p>I'm trying to mock a chained Django model object in the following dummy method, </p>
  
  <pre><code># utils.py
  def foo_something_exist():
     if Foo.objects.get(a='something').exist():
        return 'exist'
  
  
  
  # test.py
  import unittest.mock import patch
  
  import utils
  
  .
  .
  .
  @patch('utils.Foo')
  def test_foo_something_exist_returns_none(self, mock_foo):
     mock_foo.objects.get.exists.return_value = False
     self.assertIsNone(utils.foo_something_exist()) 
  .
  .
  .
  </code></pre>
  
  <p><code>test_foo_something_exist()</code> fails the test. I discovered that <code>Foo.objects.get(a='something').exist()</code> in utils.py was a MagicMock object (<code>&lt;MagicMock name='Member.objects.get().exists()' id='xxxxxx'&gt;</code>) instead of <code>False</code>, which contributed to the failure of this test function. And yes, I've also tried <code>mock_foo.objects.return_value.get.return_value.exists.return_value = False</code>, which was mentioned in a post. Pointers/hints for correctly mocking a model object (with its chained methods) is appreciate.</p>
  
  <p>Thank you in advance. </p>