From b157ffdbb97576a60995e719575ee733bba17508 Mon Sep 17 00:00:00 2001 From: Nick Sandford Date: Thu, 24 Jul 2014 21:24:01 +0100 Subject: [PATCH] Fixed #22849 -- Added Session.__str__() --- django/contrib/sessions/models.py | 5 +++++ django/contrib/sessions/tests.py | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/django/contrib/sessions/models.py b/django/contrib/sessions/models.py index 674118f216..8aad470cf3 100644 --- a/django/contrib/sessions/models.py +++ b/django/contrib/sessions/models.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals from django.db import models +from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ @@ -20,6 +21,7 @@ class SessionManager(models.Manager): return s +@python_2_unicode_compatible class Session(models.Model): """ Django provides full support for anonymous sessions. The session @@ -48,6 +50,9 @@ class Session(models.Model): verbose_name = _('session') verbose_name_plural = _('sessions') + def __str__(self): + return self.session_key + def get_decoded(self): return SessionStore().decode(self.session_data) diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py index 144b709166..40a38a9215 100644 --- a/django/contrib/sessions/tests.py +++ b/django/contrib/sessions/tests.py @@ -24,6 +24,7 @@ from django.test import TestCase, RequestFactory, override_settings from django.test.utils import patch_logger from django.utils import six from django.utils import timezone +from django.utils.encoding import force_text from django.contrib.sessions.exceptions import InvalidSessionKey @@ -310,6 +311,16 @@ class DatabaseSessionTests(SessionTestsMixin, TestCase): backend = DatabaseSession + def test_session_str(self): + "Session repr should be the session key." + self.session['x'] = 1 + self.session.save() + + session_key = self.session.session_key + s = Session.objects.get(session_key=session_key) + + self.assertEqual(force_text(s), session_key) + def test_session_get_decoded(self): """ Test we can use Session.get_decoded to retrieve data stored