title: How to set a custom field in a Django session model?
body: <p>I have extended the session in Django per <a href="https://docs.djangoproject.com/en/1.10/topics/http/sessions/#example" rel="noreferrer">this</a>. In short, I have added a field named <code>account_id</code> to hold the user id of the user which the session belongs to.</p>
  
  <p>Everything works fine, except the custom field <code>account_id</code> I have added is not being set on login. To do so:</p>
  
  <pre><code>from django.contrib import auth
  from MyApp.models import CustomSession
  
  def login(request):
      username = request.POST['username']
      password = request.POST['password']
      user = auth.authenticate(username=username, password=password)
      if user is not None:
          try:
              auth.login(request, user)
              session_key = request.session.session_key
              # CODE HERE
              if session_key is not None:
                  return HttpResponse(json.dumps({'status': 'Success'}))
              else:
                  return HttpResponse(json.dumps({'status': 'Fail'}))
  </code></pre>
  
  <p>I have tried putting the following to <code>CODE HERE</code>. However, none of them worked:</p>
  
  <ol>
  <li><p><code>request.session.model.account_id = user.id</code></p></li>
  <li><pre><code>session = CustomSession.objects.get(pk=session_key)
  session.account_id = user.id
  session.modified = True
  </code></pre></li>
  <li><code>request.session.account_id = user.id</code></li>
  <li><code>request.session[account_id] = user.id</code></li>
  </ol>
  
  <p>In each of these attempts, <code>account_id</code> is <code>NULL</code> in the data base.</p>
  
  <p>What am I missing here?</p>