title: How to mock the get_object method of a Django Rest Framework view?
body: <p>I am playing with the <code>build</code> (as opposed to <code>create</code>) method of FactoryBoy in Django.  This creates objects without storing them in the database.</p>
  
  <p>Therefore, in order for the tests of my views to work, I need to patch the methods that touch the database.</p>
  
  <p>Here is some code... </p>
  
  <p><strong>models.py:</strong></p>
  
  <pre><code>class Book(models.Model):
    title = models.CharField(max_length=100)
  </code></pre>
  
  <p><strong>serializers.py:</strong></p>
  
  <pre><code>class BookSerializer(serializers.ModelSerializer):
    class Meta:
      model = Book
      fields = ('name')
  </code></pre>
  
  <p><strong>views.py:</strong></p>
  
  <pre><code>class BookViewSet(viewsets.ModelViewSet):
  
      def get_queryset(self):
          return Book.objects.all()
  
      def get_serializer_class(self):
           return BookSerializer
  </code></pre>
  
  <p><strong>tests.py:</strong></p>
  
  <pre><code>class BookFactory(factory.DjangoModelFactory):
    title = factory.Faker('sentence', nb_words=4)
    class Meta:
      model = "Book"
  
  def my_test():
    client = APIClient()
    books = BookFactory.build_batch(10)
    list_url = reverse("books-list")
    with patch.object(BookViewSet, "get_queryset", return_value=books):
      list_url = reverse("books-list")
      response = client.get(list_url)
      # this works
      assert response.content = &lt;a list of books&gt; 
    with patch.object(BookViewSet, "get_object", return_value=books[0]):
      detail_url = reverse("books-detail", args=123)
      response = client.get(detail_url)
      # this is always empty..
      assert response.content == &lt;a book&gt;
  </code></pre>
  
  <p>No matter what I try, the detail-view always returns empty JSON.  Am I using <code>patch</code> wrong?</p>