title: How to make a class JSON serializable for usage in Django session
body: <p>I try to store in Django session the following pretty much arbitrary class:</p>
  
  <pre><code>class QuizInfo:
      def __init__(self):
          self.i_active_question = INVALID_PQA_ID  # integer
          self.sequence = []
  </code></pre>
  
  <p>In turn, each item of <code>QuizInfo.sequence</code> list is an instance of <code>AnsweredQuestion</code>:</p>
  
  <pre><code>class AnsweredQuestion:
      def __init__(self, i_question, i_answer):
          self.i_question = i_question
          self.i_answer = i_answer
  </code></pre>
  
  <p>I try to store such a data structure in <code>HttpRequest.session</code> like the following:</p>
  
  <pre><code>qi = QuizInfo()
  qi.i_active_question = 5  # e.g.
  qi.sequence.append(AnsweredQuestion(1, 2))  # e.g.
  qi.sequence.append(AnsweredQuestion(3, 4))  # e.g.
  quiz_id = 7  # e.g.
  request.session['quiz_infos'] = {quiz_id : qi}
  </code></pre>
  
  <p>So <code>request.session['quiz_infos']</code> is a dictionary of <code>int</code> keys and <code>QuizInfo</code> values.</p>
  
  <p>I get the following error:</p>
  
  <pre><code>Traceback:
  
  File "C:\Users\Sarge\Envs\PqaWeb1\lib\site-packages\django\core\handlers\exception.py" in inner
    34.             response = get_response(request)
  
  File "C:\Users\Sarge\Envs\PqaWeb1\lib\site-packages\django\utils\deprecation.py" in __call__
    93.             response = self.process_response(request, response)
  
  File "C:\Users\Sarge\Envs\PqaWeb1\lib\site-packages\django\contrib\sessions\middleware.py" in process_response
    58.                             request.session.save()
  
  File "C:\Users\Sarge\Envs\PqaWeb1\lib\site-packages\django\contrib\sessions\backends\db.py" in save
    83.         obj = self.create_model_instance(data)
  
  File "C:\Users\Sarge\Envs\PqaWeb1\lib\site-packages\django\contrib\sessions\backends\db.py" in create_model_instance
    70.             session_data=self.encode(data),
  
  File "C:\Users\Sarge\Envs\PqaWeb1\lib\site-packages\django\contrib\sessions\backends\base.py" in encode
    96.         serialized = self.serializer().dumps(session_dict)
  
  File "C:\Users\Sarge\Envs\PqaWeb1\lib\site-packages\django\core\signing.py" in dumps
    87.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
  
  File "c:\programs\python37\Lib\json\__init__.py" in dumps
    238.         **kw).encode(obj)
  
  File "c:\programs\python37\Lib\json\encoder.py" in encode
    199.         chunks = self.iterencode(o, _one_shot=True)
  
  File "c:\programs\python37\Lib\json\encoder.py" in iterencode
    257.         return _iterencode(o, 0)
  
  File "c:\programs\python37\Lib\json\encoder.py" in default
    179.         raise TypeError(f'Object of type {o.__class__.__name__} '
  
  Exception Type: TypeError at /
  Exception Value: Object of type QuizInfo is not JSON serializable
  </code></pre>
  
  <p>How to make the data structure JSON-serializable?</p>
  
  <p>I saw the other questions, however, the answers there mostly suggest to implement a method to pass as a parameter. But where to do this in Django?</p>