Skip to content

Commit a931ece

Browse files
Add support for custom User subclasses (#171)
## Description This PR changes the behavior of the `DescopeUser` model somewhat. Previously the `DescopeUser` model subclassed directly from Django's built-in `User` model, and was configured as a proxy model. This works fine if you only intend to use the Django built-in model, however this does not work if a user wants to define a custom model via the `AUTH_USER_MODEL` setting. This PR makes use of the `get_user_model` function from Django, and subclasses from that, which means that if a user specifies a custom model from their application in the `AUTH_USER_MODEL` setting, then the `DescopeUser` model will subclass from that. ## The two important things to note `DescopeUser` is no longer a proxy model, this functionally I don't think changes much, but does mean it creates it's own DB table, wether it adds any new fields or not. This has been changed because in Django, you can not create a proxy model off of another model which is not a base model. So for example, the proxy model would work with Django's built-in user as that is a base model, but it would not work on a custom user model that subclasses from Django's built-in. This does mean that the descope migrations can not be pre-shipped, and must be generated during the user application's run of `makemigrations`, because the DescopeUser migration will be dependent on the application's User model(and this may be Django's built-in, but in this scenario django-descope can't know that until the user creates their migrations). --------- Co-authored-by: Omer Cohen <omer@descope.com>
1 parent b843bdc commit a931ece

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

django_descope/migrations/0001_initial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import django.contrib.auth.models
44
from django.db import migrations
5+
from django.conf import settings
56

67

78
class Migration(migrations.Migration):
8-
99
initial = True
1010

1111
dependencies = [
@@ -21,7 +21,7 @@ class Migration(migrations.Migration):
2121
"indexes": [],
2222
"constraints": [],
2323
},
24-
bases=("auth.user",),
24+
bases=(settings.AUTH_USER_MODEL,),
2525
managers=[
2626
("objects", django.contrib.auth.models.UserManager()),
2727
],

django_descope/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22

33
from descope import SESSION_TOKEN_NAME
4-
from django.contrib.auth import models as auth_models
4+
from django.contrib import auth
55
from django.core.cache import cache
66

77
from . import descope_client
@@ -10,7 +10,7 @@
1010
logger = logging.getLogger(__name__)
1111

1212

13-
class DescopeUser(auth_models.User):
13+
class DescopeUser(auth.get_user_model()):
1414
class Meta:
1515
proxy = True
1616

0 commit comments

Comments
 (0)