[Django]-Using session key of Django session as foreign key

7👍

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

Leave a comment