zinc
zinc

Reputation: 81

Using session key of Django session as foreign key

I have used session in Django and this resulted in creation of table django_session in MySQL. I want to use the session_key of this table as foreign key in other MySQL tables. How can I do this?

Upvotes: 3

Views: 2323

Answers (1)

Alex Pavlenko
Alex Pavlenko

Reputation: 469

from django.contrib.sessions.models import Session

class MyModel(models.Model):
    ...
    session = models.ForeignKey(Session, on_delete=models.SET_NULL, blank=True, null=True)
    ...

Later on you can get session key of interest from your objects like the following:

my_model_object.session.session_key

Upvotes: 7

Related Questions