简体   繁体   中英

Django different USERNAME field for registration and for login

I want to have a USERNAME_FIELD for registration of my users (lets say this field is the country ID) and another one for login (internal id).

My process is as follows:

  • User registers with name, email and COUNTRY_ID (as username_field) aswell with a password.

  • Next, an admin validates this information and confirms the registration of this user and an automatic internal ID is assigned.

  • The user is then informed (via email) of this action and can therefore login with this internal id.

Is it possible then to have a different USERNAME_FIELD for registration and for login?

The username is supposed to be unique (obviously - how else would you identify your user at login ?), so in it's current formulation, your question doesn't really make sense. If you don't want the users to provide a username when applying for registration, then your question is not about having "a USERNAME_FIELD for registration and another for login", it's about "how to allow the user to register without providing a value for the USERNAME_FIELD".

One possible solution is to use a system-provided unique random value (ie an uuid or something built from user's ip + current timestamp + some random value) as temporary username.

Another is to use a distinct model for registrations and only create the User during validation process (which, if you're using the default User model, will help telling the yet unvalidated registrations).

In both cases you'll of course have to write your own forms and views (preferably delegating what can be to the contrib.auth app - don't try to reinvent the wheel either).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM