---
slug: "django-change-user-on-session"
title: "Switching the Logged-in User in Django Using the Django Shell"
description: "Here's an overview in English:\n\nHow to change the user of an already established session using the Django shell.\n\nPlease do not attempt this in a production environment as it can be dangerous."
url: "https://www.ytyng.com/en/blog/django-change-user-on-session"
publish_date: "2023-10-12T12:41:35Z"
created: "2023-10-12T12:41:35Z"
updated: "2026-02-26T04:43:56.897Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250615/0520dfca7e0c4e72a2420ff1c6bebca4.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/294/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/294/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/294/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/294/featured-music-294-2.mp3", "https://media.ytyng.net/ytyng-blog/294/featured-music-294-3.mp3"]
lang: "en"
---

# Switching the Logged-in User in Django Using the Django Shell

## How to Force Login as Another User for Inquiry Handling

When performing actions such as handling inquiries, be extremely cautious as incorrect operations or leaving the session logged in as another user can be dangerous. Do not perform these actions in a production environment.

The author assumes no responsibility for any impacts, damages, or issues that arise from performing these actions.

1. First, log in to the Django application using your browser.
2. Record the `session_id` from the cookies.
3. Execute `./manage.py shell` on the server.

```python
# Your own session_id
session_id = 'abcd1234xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# ID of the user you want to impersonate
user_id = 1

from django.conf import settings
from user.models import User

engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])

session = engine.SessionStore(session_id)

dict(session)  # Confirm your own user ID

user = User.objects.get(id=user_id)
user  # Verify the user details

session['_auth_user_id'] = user.id
session['_auth_user_hash'] = user.get_session_auth_hash()
session.save()
```

Reload the browser, and you should be logged in as the desired user.

After completing the verification tasks, make sure to delete the cookies and log back in with the regular user to avoid any accidents.
