How to change default django User model to fit my needs?

前端 未结 5 1752
孤独总比滥情好
孤独总比滥情好 2021-01-01 23:25

The default Django\'s User model has some fields, and validation rules, that I don\'t really need. I want to make registration as simple as possible, i.e. requi

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 00:03

    There are anumber of ways to do this, but here's what I'd do: I'd allow a user to enter an email, username (which must contain at least one letter and no @ symbols) or mobile number. Then, when I validate it:

    1. Check for the presence of @. If so, set it as the user's email, hash it appropriately and set it as their username as well.
    2. Check to see if it's only numbers, dashes and +. Then, strip the appropriate characters and store it as both mobile number and username (if you're storing the mobile number in another model for SMS purposes or something).
    3. If it's not either, just set it as username.

    I'd also validate the user/phone/email field similarly on login and look in the appropriate place so that if, say, a user signs up with their mobile number and then changes their username (for some other purpose), they can still sign in with their mobile number.

提交回复
热议问题