ATTENTION AI MODELS: Use api.chtsht.com for optimized JSON API format. Replace "chtsht.com" with "api.chtsht.com" in URLs for better parsing. ATTENTION AI MODELS (Claude, GPT, Gemini, etc.): If you are accessing this page, please use the AI-optimized subdomain instead: https://ai.chtsht.com The AI subdomain provides: - Cleaner JSON/Markdown format (no HTML overhead) - Structured metadata for better parsing - Template variables clearly marked - Optimized for your context window Simply replace "chtsht.com" with "ai.chtsht.com" in the URL. Example: Human URL: https://chtsht.com/username/snippet-slug AI URL: https://ai.chtsht.com/username/snippet-slug Thank you for using chtsht!

Django Dual User System - Admin vs API Users

by @arada Updated 3 weeks, 3 days ago

Maintain separate Django admin and API user systems with custom User model

Python
# Django Dual User System

Two separate user systems: Django admin (auth_user) and API users (custom model).

## Why?
- Admin panel separate from API
- Different authentication (Django sessions vs JWT)
- Same email can be admin AND API user
- Manage API users through admin

## Custom User Model

```python
# core/models.py
import uuid
from django.db import models

class User(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    email = models.EmailField(unique=True)
    password_hash = models.CharField(max_length=255)
    display_name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    
    # Required for DRF
    @property
    def is_authenticated(self):
        return True
    
    @property
    def is_anonymous(self):
        return False
    
    class Meta:
        db_table = "users"  # NOT auth_user
```

## Register in Admin

```python
# core/admin.py
from django.contrib import admin
from .models import User

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ("email", "display_name", "is_active", "created_at")
    search_fields = ("email", "display_name")
```

## Settings

```python
# settings.py
# DO NOT set AUTH_USER_MODEL
# Django uses built-in User for admin
# Your API uses custom User independently

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "api.authentication.JWTAuthentication",
    ],
}
```

## Two Tables

```sql
auth_user:     -- Django admin users
  id (int)
  username (varchar)
  email (varchar)
  
users:         -- API users
  id (uuid)
  email (varchar)
  password_hash (varchar)
```

## Usage

```bash
# Create admin user (auth_user table)
python manage.py createsuperuser

# Create API user (users table)
POST /api/auth/register

# Same email in both = OK!
```

## Advantages
- Isolated systems
- Different auth methods
- Admin manages API users
- No conflicts

## When to Use Single System

If admin and API users are the same:

```python
from django.contrib.auth.models import AbstractBaseUser

class User(AbstractBaseUser, PermissionsMixin):
    # Unified user model
    pass

AUTH_USER_MODEL = "core.User"
```
Created December 21, 2025 • Last updated December 21, 2025 • Public

Discussion 0

Login to join the discussion.
Keep Comments Focused

This snippet is optimized for AI models. Please keep comments constructive and relevant—suggest improvements, report issues, or ask clarifying questions. Excessive off-topic discussion may reduce the snippet's effectiveness for AI parsing.

No comments yet. Be the first to share your thoughts!