Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:17:44+00:00 2026-06-08T19:17:44+00:00

Does Django flattening response after process_response? I have this model (simplified version): class Ticket(models.Model):

  • 0

Does Django flattening response after process_response?

I have this model (simplified version):

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)

and middleware (simplified):

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 > 0:
            request.ticket.remains -= 1
            if request.ticket.remains == 0:
                request.ticket.is_active = False
            request.ticket.save()
        return response

In first case I got forbidden page on fourth request:

   RESPONSE     PRINT
1: True         True 2
2: False        True 1
3: False        True 1
4: Forbidden

In second case I got forbidden page on third request:

   RESPONSE     PRINT
1: True         True 2
2: True         True 1
3: Forbidden

And this second case is the way I want. Why I must stringify my model first for properly flow? What I misunderstood?

django.VERSION = (1, 2, 3, 'final', 0)

EDIT

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:

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:17:46+00:00Added an answer on June 8, 2026 at 7:17 pm

    I really doubt that the first case displays two times False. I’ve made a simple test with your code, and it displays for the first case:

    1: True
    2: False
    3: Forbidden
    

    which is the expected behavior. As long as the process_response modifies the is_active data and saves the ticket on the second call, I don’t see how a third call can return False… Unless the ticket.remains is modified somehow. With the code shown here, I can’t see how it is possible that process_response to save the ticket with is_active=False, and on subsequent request process_request not to redirect to Forbidden… Which Django version do you use? Print also ticket.remains in the __unicode__() to see how it changes …

    The reason that you get False in the second call with 1st case is that you pass a model instance

    return HttpResponse(request.ticket)
    

    The ticket.__unicode__() is being called after the process_response is being called, during the final writing of the contents of HttpResponse() to the client. And since in the first scenario ticket.is_active is being modified in process_response, when ticket.__unicode__() is finally called, it gets the modified value of ticket.is_active which is False.

    In the second scenario the __unicode__ is called explicitly in process_request and evaluated to ‘True’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model, Rating , which looks something like this: class Rating(models.Model): upvotes
I have a Python program (with Django - does this matter?) that I want
How does django-admin resolves model from url? I takes url like this /admin/video/item/ How
How does Django decide what in the model will be assigned an _id, when
It looks like Django does not update last_login field in auth_user model when a
I am doing geolocation, and Django does not have a PointField. So, I am
django-userena does not have a views.py file. It uses instead html files, i would
Why does Django give me this exception [(7, u'Acura'), (18, u'Alfa Romeo'), ...] is
How well does Django's anti-spam system in the comments framework work? Have you used
I feel like I am writing something that should already exist. Does Django have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.