Using sessions in Django

460 views
Skip to first unread message

Sandeep kaur

unread,
Aug 17, 2012, 3:58:55 AM8/17/12
to greatdevelopers, django-users
I am using session foreign key in my table.

class ClientJob(models.Model): #models.py
.....................................
sess = models.ForeignKey(Session)

And in order to save its value in database, I used ,.

def add_job(request) #views.py
...................................
if not request.session.exists(request.session.session_key):
request.session.create()
profile.sess = request.session.session_key
profile.save()

But when I execute my form and click on submit button, I get error as :

```Cannot assign "'b593c61453d7aad199078c66b9ad6b30'":
"ClientJob.sess" must be a "Session" instance.````

I am a bit unclear about using sessions in django, so be a little elaborative.

Thank you.


--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com

Amyth Arora

unread,
Aug 17, 2012, 4:09:45 AM8/17/12
to django...@googlegroups.com
I think this is because you are passing it the "Session Key" instead
of the "Session Instance", you need to get the session instance and
then pass it to your function, Something like this:

profile.sess = Session.objects.get(session_key=request.session.session_key)
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>



--
Best Regards,

Amyth Arora
http://techstricks.com

Thomas Orozco

unread,
Aug 17, 2012, 4:12:20 AM8/17/12
to django...@googlegroups.com

Session key is the session 's primary key. Not the session itself.

What you want to assign to your field is a session, not its key. So use request.session.

However.

Please very careful when using FK' s to sessions, sessions are meant to be short lived, and could / should be deleted once they expire (ie. When the user logs off or after a specific duration), depending on your backend.

I'm not sure what you're trying to achieve, but I think that FKing to sessions is, at the very list, pretty risky.

Especially given that the default on delete behavior of a ForeignKey is cascade.

Sandeep kaur

unread,
Aug 17, 2012, 4:26:43 AM8/17/12
to django...@googlegroups.com
On Fri, Aug 17, 2012 at 1:42 PM, Thomas Orozco
<g.orozc...@gmail.com> wrote:
> Session key is the session 's primary key. Not the session itself.
>
> What you want to assign to your field is a session, not its key. So use
> request.session.
>
> However.
>
> Please very careful when using FK' s to sessions, sessions are meant to be
> short lived, and could / should be deleted once they expire (ie. When the
> user logs off or after a specific duration), depending on your backend.
>
> I'm not sure what you're trying to achieve, but I think that FKing to
> sessions is, at the very list, pretty risky.
>
> Especially given that the default on delete behavior of a ForeignKey is
> cascade.

You are right.
Actually, I want a cart like system. Like in my project's case, if a
client want to test multiple material in one job, it should generate
one bill and receipt. But for each different material, different jobs
are created. So, I thought it could be achieved by storing the session
in which the user logged and then get the bill generated based on that
session.
If you find it a vague solution, do tell the most optimum solution.

Thomas Orozco

unread,
Aug 17, 2012, 4:32:58 AM8/17/12
to django...@googlegroups.com

I'm sorry, I have some trouble understanding exactly what you need, could you please try and provide a bit more context ? :-)

Tom Evans

unread,
Aug 17, 2012, 4:34:10 AM8/17/12
to django...@googlegroups.com
On Fri, Aug 17, 2012 at 9:12 AM, Thomas Orozco
<g.orozc...@gmail.com> wrote:
> Session key is the session 's primary key. Not the session itself.
>
> What you want to assign to your field is a session, not its key. So use
> request.session.
>

This would not work; request.session is a
django.contrib.sessions.backends,SessionBase subclass, and not a model
instance.

You would need to use the advice Amyth gave you.

Cheers

Tom

Thomas Orozco

unread,
Aug 17, 2012, 4:39:13 AM8/17/12
to django...@googlegroups.com

My mistake then, sorry about that!

Sandeep kaur

unread,
Aug 17, 2012, 4:58:07 AM8/17/12
to django...@googlegroups.com
On Fri, Aug 17, 2012 at 1:39 PM, Amyth Arora <aroras....@gmail.com> wrote:
> I think this is because you are passing it the "Session Key" instead
> of the "Session Instance", you need to get the session instance and
> then pass it to your function, Something like this:
>
> profile.sess = Session.objects.get(session_key=request.session.session_key)
>
Thanks a lot. This worked. :)

Amyth Arora

unread,
Aug 17, 2012, 5:01:32 AM8/17/12
to django...@googlegroups.com
You're Welcome. Glad to Help.

Sandeep kaur

unread,
Aug 17, 2012, 8:46:24 AM8/17/12
to greatdevelopers, django-users
On Fri, Aug 17, 2012 at 1:28 PM, Sandeep kaur <mkaur...@gmail.com> wrote:
> I am using session foreign key in my table.

> if not request.session.exists(request.session.session_key):
> request.session.create()
> profile.sess = request.session.session_key
> profile.save()
>
> But when I execute my form and click on submit button, I get error as :
>
> ```Cannot assign "'b593c61453d7aad199078c66b9ad6b30'":
> "ClientJob.sess" must be a "Session" instance.````
>

Problem solved!!
Used this code :

if not request.session.exists(request.session.session_key):
request.session.create()
profile.sess = Session.objects.get(session_key=request.session.session_key)

Andreas

unread,
Sep 24, 2016, 4:06:26 PM9/24/16
to Django users, greatde...@googlegroups.com
Thanks for the help, although i get an error, saying "DataError: value too long for type character varying(2000)" when i am trying to save the "Session.objects.get(session_
key=request.session.session_key)".

 I checked my db and the "session_id" column is varchar(40) (which is the default value that created because of the "models.ForeignKey(Session)" ).
Any suggestions on how to fix that? I did exactly the same thing as you guys suggested before.
Reply all
Reply to author
Forward
0 new messages