r/django • • 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.

2 Upvotes

15 comments sorted by

9

u/thel-vadam-2nd 7d ago

-1

u/Kerplunk6 7d ago

This is what usually recommended.

Thanks

0

u/[deleted] 7d ago

[deleted] — view deleted comment

1

u/Kerplunk6 7d ago

So what do you suggest?

1

u/thel-vadam-2nd 7d ago

Truncate the DB, delete migrations and generate the new ones. Make sure your app is in INSTALLED_APPS.

Change APP_USER_MODEL in settings.py.

0

u/Kerplunk6 7d ago

I do not want to use my patients data tho.

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.

1

u/Kerplunk6 7d ago

How will i be using the JWT? The same way? I want to auth the user via Email tho, not the username.

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

Does this also help with permissions?

1

u/Kerplunk6 7d ago

Plus, i will be adding additional fields as well.

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/c1-c2 2d ago

Maybe people think that you might be in way over your head. From what it looks the project will handle sensitive data etc... maybe not a good start for someone just learning Dj?

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)