Using different table for authentication and simple jwt - Django rest framework

Im a bit new to Django REST and Django as a whole, how do you use a different table for auth since my api is looking for auth_user , it keeps saying "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'auth_user'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)" when I try to add my jwt token bearer to an [isAuthenticated] apiview. by the way my django uses mssql

I tried adding AUTH_USER_MODEL to my model but it says the same error

To use a different user table for auth in Django, you need to ensure that the AUTH_USER_MODEL setting is properly configured. So you need to define a custom user model and tell django that you want to use it

in your models.py, try something like that :

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models

class CustomUserManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):
        if not email:
            raise ValueError("The Email field must be set")
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password=None, **extra_fields):
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", True)

        return self.create_user(email, password, **extra_fields)

class YourCustomUser(AbstractBaseUser):
    email = models.EmailField(unique=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)

    objects = CustomUserManager()

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email

Adapt the model to your need

And after the creation of the customUser, you need to tell Django that you want to use it :

In your settings.py of the project, find this line and point your model :

AUTH_USER_MODEL = 'your_app_name.YourCustomUser'

Then do your migrations

Back to Top