title: Middleware: using model in process_request and modifying it in process_response
body: <p>Does Django flattening response after process_response?</p>
  
  <p>I have this model (simplified version):</p>
  
  <pre><code>class Ticket(models.Model):
      uuid = UUIDField(primary_key = True)
      is_active = models.BooleanField(default = False)
      remains = models.IntegerField(default = 2)
  
      def __unicode__(self):
          return '%s' % (self.is_active)
  </code></pre>
  
  <p>and middleware (simplified):</p>
  
  <pre><code>class TicketMiddleware(object):
      def process_request(self, request):
          # ...
          try:
              request.ticket = Ticket.objects.get(uuid_exact=request.GET['ticket'], is_active=True)
              print '%s %d' % (request.ticket.is_active, request.ticket.remains)
              # first case:
              return HttpResponse(request.ticket)
              # second case:
              #return HttpResponse(request.ticket.is_active)
          except Ticket.DoesNotExists:
              return HttpResponseForbidden()
  
      def process_response(self, request, response):
          if request.ticket is not None and request.ticket.remains &gt; 0:
              request.ticket.remains -= 1
              if request.ticket.remains == 0:
                  request.ticket.is_active = False
              request.ticket.save()
          return response
  </code></pre>
  
  <p>In first case I got forbidden page on fourth request:</p>
  
  <pre><code>   RESPONSE     PRINT
  1: True         True 2
  2: False        True 1
  3: False        True 1
  4: Forbidden
  </code></pre>
  
  <p>In second case I got forbidden page on third request:</p>
  
  <pre><code>   RESPONSE     PRINT
  1: True         True 2
  2: True         True 1
  3: Forbidden
  </code></pre>
  
  <p>And this second case is the way I want. Why I must stringify my model first for properly flow? What I misunderstood?</p>
  
  <pre><code>django.VERSION = (1, 2, 3, 'final', 0)
  </code></pre>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I found the problem. In real code I use foreign key for django.contrib.auth.models.User and unicoding my Ticket model with username of associated User:</p>
  
  <pre><code>class Ticket(models.Model):
      # ...
      user = models.ForeignKey(User)
  
      def __unicode__(self):
          # this works as expected
          return u'%s' % (self.is_active)
          # with this I have unexpected third step in first case
          # but I dont understand why
          #return u'%s %s' % (self.user.username, self.is_active)
  </code></pre>