---
title: "Using session key of Django session as foreign key"  
description: "Using session key of Django session as foreign key"  
author: "Sandra Emily"  
published: 2023-07-27  
updated: 2023-07-28  
canonical: https://www.mindstick.com/forum/159323/using-session-key-of-django-session-as-foreign-key  
category: "python"  
tags: ["python", "django"]  
reading_time: 2 minutes  

---

# Using session key of Django session as foreign key

Using the [session](https://www.mindstick.com/articles/12042/session-in-c-sharp) key of the [Django](https://www.mindstick.com/articles/337468/the-pros-and-cons-of-using-django-for-web-development-projects) session as a [foreign key](https://www.mindstick.com/blog/174/foreign-key-self-reference-constraint)

## Replies

### Reply by Aryan Kumar

Sure, you can use the session key of Django as a [foreign](https://yourviews.mindstick.com/story/2164/here-are-the-best-foreign-universities-for-higher-education) key by creating a new model that inherits from the `django.contrib.sessions.models.Session` model and adding a foreign key to the `user` model.

The following code shows how to do this:

Python

```plaintext
from django.contrib.sessions.models import Session
from django.db import models

class SessionUser(models.Model):
    session_key = models.ForeignKey(Session, on_delete=models.CASCADE)
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
```

This code creates a new model called `SessionUser` that inherits from the `django.contrib.sessions.models.Session` model. The `session_key` field is a foreign key to the `Session` model, and the `user` field is a foreign key to the `auth.User` model.

To use the `session_key` field as a foreign key, you would need to create a new model that inherits from the `SessionUser` model. For example, the following code shows how to create a new model called `Order` that inherits from the `SessionUser` model:

Python

```plaintext
from django.db import models

class Order(SessionUser):
    order_number = models.CharField(max_length=100)
    total_price = models.DecimalField(max_digits=10, decimal_places=2)
```

This code creates a new model called `Order` that inherits from the `SessionUser` model. The `order_number` field is a string field that stores the order number, and the `total_price` field is a decimal field that stores the total price of the order.

You can then use the `session_key` field to query the `Order` model. For example, the following code shows how to query the `Order` model for all orders that were placed by the user with the session key `1234567890`:

Python

```plaintext
from django.db.models import Q

orders = Order.objects.filter(session_key=1234567890)
```

This code will return a list of all orders that were placed by the user with the session key `1234567890`.


---

Original Source: https://www.mindstick.com/forum/159323/using-session-key-of-django-session-as-foreign-key

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
