[Answered ]-Django: Encrypted password to legacy (not auth_user) table

2👍

✅

Well you haven’t mentioned what your legacy table looks like, this answer assumes that it has the basic username, password and email fields. if you are storing passwords in plain text at the moment it can be hashed but if you are using some kind of third party solution, each user will have to reset his/her password.

import account.models import User as LegacyUser
import django.contrib.auth.models User

users = []
for u in LegacyUser.objects.all():
    new_user = User(username=u.user, email=u.email)
    new_user.set_password(u.password)
    users.append(new_user)

User.objects.bulk_create(users)

the above is if you want to salvage passwords, if you don’t just do this:

INSERT INTO auth_user(username,password,email) SELECT username, password, email FROM account_user

in psql or pgadmin or whatever. This should be much faster.

Also, I would like to keep using the users table for clients/customers
and keep the auth_user for users of /admin… unless my thinking is
wrong on this.

Sorry to say your thinking is wrong on this. There is no need for two tables because the django.contrib.auth.User table has two fields is_staff and is_superuser specifically designed to deal with this situation.

Alternatively, you can ditch the django.contrib.auth.models.User model completely and use your user model as the default. See Specifying a custom user model having two models is just duplicating code and work and potentially introducing security problems

Leave a comment