0

I have an application that uses the requests library to make calls to a web service. On django, I use a table to keep sessions using the standard django_session library.

I've noticed that I have a new record in the database for every time the requests library fires off a call. I find this quite bizarre, since I'm not using request sessions (explicitly) and most of my calls are single GET calls that shouldn't need any kind of persistance.

Has anybody else had this problem?

Jeremy
  • 1
  • 85
  • 340
  • 366
Gevious
  • 3,214
  • 3
  • 21
  • 42
  • When you fire the GET request with requests library, it will pass in the session middleware. At this point, the middleware recognizes that there is no session cookie and it tries to create a new session ID. I think is this behaviour which is causing that database session write. That's not really a problem its just the way how django works. – andrefsp Oct 25 '12 at 15:18

2 Answers2

2

django.contrib.sessions.middleware.SessionMiddleware will create a new session for each request.

If you want to disable this behavior, consider replacing django.contrib.sessions.middleware.SessionMiddleware with a custom middleware that extends django.contrib.sessions.middleware.SessionMiddleware but prevents sessions from being created in instances where you don't want them.

This answer provides an example of how to do so. It parses request.path_info to determine whether a session should be created, but you could use a number of different techniques, such as adding a custom header to your request, including a post variable, etc.

Community
  • 1
  • 1
dgel
  • 16,352
  • 8
  • 58
  • 75
1

Turns out I had this setting turned on

SESSION_SAVE_EVERY_REQUEST=True

When I started with sessions I wanted to be safe to make sure nothing falls through the cracks. Turns out this was then writing empty session information to the db.

Gevious
  • 3,214
  • 3
  • 21
  • 42