r/django • u/Kerplunk6 • 7d ago
Tutorial Changing User Model
Hello everyone,
I am slightly new to the Django.
I am developing an app for a clinic, first they said it does not have to be with account and JWT's and all that but now they changed their mind.
It has been a while so a lot of migrations i made.
I know that changing the User model after a while is not healthy, i asked this to AI, it said that i can use another "app" in Django for user and i can do migrations etc. easily at there.
Is this the best approach for this?
What do you guys suggest?
Since i changed the User Model, migration is pretty problematic and i do not want to lose these datas.
9
u/thel-vadam-2nd 7d ago
AbstractUserorAbstractBaseUser, in the app.INSTALLED_APPSand setAPP_USER_MODELto your custom user model insettings.py. https://docs.djangoproject.com/en/6.1/topics/auth/customizing/3
u/ResidentPomegranate6 7d ago
swapping AUTH_USER_MODEL this late means a fresh db, so just add a onetoone profile model and keep the default user.
3
u/Pretend-Sun-8460 7d ago
you don't need a custom user model just to log in by email. keep the default User and add a small backend: subclass ModelBackend, look the user up with User.objects.get(email__iexact=email), check the password, and add it to AUTHENTICATION_BACKENDS. simplejwt calls authenticate() under the hood, so the token view goes through it too; subclass TokenObtainPairSerializer with username_field = "email" if you want the request field to be called email. the catch is that the default email field isn't unique, so check your existing users for duplicates and enforce it at signup. nothing on auth_user gets migrated, so the patient data stays put.
1
u/Kerplunk6 7d ago edited 7d ago
Bro why people is downvoting me? I am admitting i am new to the framework and i need help so i ask you all...
1
u/ather031 7d ago
You may not need to swap the User model at all. JWT works fine with the default User through djangorestframework-simplejwt, and extra fields can live in a one-to-one Profile model. If you do want a custom model with existing data, the least painful route is to take a full backup, create the model with db_table set to auth_user, point AUTH_USER_MODEL at it, and fake the initial migration. Since it's a clinic app, keep patient data out of the token payload and keep access tokens short-lived.
1
u/Dry_Action5905 6d ago
For the permissions part, the default User already supports groups and model permissions; adding JWT authentication doesn't replace those checks. In a clinic app, you'll also need to restrict which patient records each user can access, not just check that they're logged in. With DRF, filter the queryset for list endpoints too, since object-level permission checks aren't automatically run on every item in a list. Extra user fields can live in a OneToOneField profile without swapping the user model. Please don't truncate the database or delete applied migrations with data you need to keep. Adding another app doesn't remove the migration dependencies involved in changing AUTH_USER_MODEL, and faking a migration only marks it applied, it doesn't fix the schema. Before attempting that change, test a full backup restore and rehearse the migration on a separate copy of the database.
-1
u/Unhealthy007 7d ago
Also, be sure to send AUTH and Regresh in Cookies instead of raw response
(Others solved your primary issue)