In Django, the default attributes for user:
username
password
email
first_name
last_name
I would like to remove email, first_name, last_name
and replace it with company
Is that possible ? Can someone show me the process of performing an authentication session with these 3 modified attributes:
- company
- username
- password
Thanks.
You should read the documentation regarding customizing authentication in Django especially the part regarding User model substitution if you would like to create your own model.
Related
I am using django REST framework.
This is my user model extended from AbstractUser
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
I am now trying to check for multiple languages my models can work.
When I validate the user object serializer using if user_serializer.is_valid(): it gives me false.
I tried entering the user details by django admin panel but there it says Email is invalid
All other fields like models.CharField work perfectly fine with other languages. But this email field does not work.
EmailField is using EmailValidator class by default which might result in an unexpected outcome. You can simply pass a custom validator class that whitelisted the domains you want. Here is the default class you might want to extend or overwrite. There is an answer with more details in StackOverflow in case you need more details.
Im using ldap for user authentication in django 2.0, and i need to create an endpoint to authenticate user from another application just passing the username to then redirect them. Isnt yet something to retrieve the raw password?
Using the **encrypt** method imported from **django_cryptography.fields**, define a password attribute for the model LdapProfile (model related to User to store the user profile on ldap) and then, use this password attribute in ldap.authenticate(username, password).
password = encrypt(models.CharField(
max_length=255,
null=True,
blank=True
))
Currently my user table looks like this - (all fields are not null)
display_name = CharField # string
email_address = EmailField (primary key) # string
password = CharField # string
However, I have decided to add additional functionality and to allow users to change their email addresses.
The flow goes like this
Does anyone have any ideas on how to do this?
Currently I am thinking of something like
display_name = CharField # string
email_address = EmailField (primary key) # string
password = CharField # string
pending_email = EmailField (unique) # string
And simply hold the new email address in pending_email before replacing the old email address in email_address
But obviously this is far from perfect (e.g. pending_email unique constraint does not cover email_address)
Ive thought about just leaving it like this and performing more selects against the database with AJAX queries to check if the desired new email address already lives in email_address before allowing it to be entered into pending_email but this seems still vulnerable to race conditions and poor user experiences on top of being not very database friendly.
The standard practice in this sort of situation is to create a separate table for email addresses. That allows users to have more than one email address at a given time and one of them can be marked as default.
This is what django-allauth's EmailAddress model looks like. In fact, unless you have a very compelling reason to write your own authentication system, I highly recommend that your swith to django allauth or any of the widely used django authentication/registration system.
class EmailAddress(models.Model):
user = models.ForeignKey(allauth_app_settings.USER_MODEL,
verbose_name=_('user'))
email = models.EmailField(unique=app_settings.UNIQUE_EMAIL,
max_length=app_settings.EMAIL_MAX_LENGTH,
verbose_name=_('e-mail address'))
verified = models.BooleanField(verbose_name=_('verified'), default=False)
primary = models.BooleanField(verbose_name=_('primary'), default=False)
objects = EmailAddressManager()
I am stuck with the schema creation so could you provide me an idea to implement this scenario
i do have 4 types of users Student, Teacher, Parent and Admin
i have tried to create 4 tables for each users and a table for username, password and tokens but i am not able to relate this table to the users because more than one user cannot have same username
What i want is i need to authenticate each user withe their user name and password and the Student table might be having relationship with Parent table !!!!
so while authenticating i need to know which type of user he/she is
i am using python Django 1.9
Instead of creating four separate tables, you can simply add a field which will reflect the user_type of the user.
class CustomUser(AbstractBaseUser):
...
[other model fields]
...
USER_TYPE_CHOICES = (
('student', 'Student'),
('teacher', 'Teacher'),
('parent', 'Parent'),
('admin', 'Admin'),
)
user_type = models.CharField(choices=USER_TYPE_CHOICES, max_length=7)
By doing this, all users will have unique usernames and they will become easy to manage too.
You can check type of user just by accessing its user_type, it'll return one of this text values "Student, Teacher, Parent and Admin". So you'll be able to handle business logic for different user types.
You can use the Django authentication system :
Using the Django authentication system
and for each category of user you can use : Groups
django.contrib.auth.models.Group models are a generic way of categorizing users so you can apply
permissions, or some other label, to those users. A user can belong to
any number of groups.
Creating four tables is not the correct way.
Alternatively you can ,
class User():
id = Column(PrimaryKey)
username = Column(String, constraints)
password = Column(String, constraints)
usertype = Column(Enum('student','teacher','parent','admin'))
-- The code is only for representation .
For setting user role , you can use the Enum Property. In case you are using SQLALchemy , check this out
How to create ENUM in SQLAlchemy?
Django auth has username and first_name fields. But username has field option unique=True. This prevents a user from giving first_name same as username when registering and raises IntegrityError. How to bypass this and also keeping username unique? ie. no two users should have the same username. But username can be same as first_name. And two users can have same first_name, last_name.
You cannot achieve that. If you want to have the same value in fields first_name and username and one of them is not unique, the other one also cannot be unique.
As far as I understand what you're doing here, you just want to display first_name instead of username - to achieve that just use {{ user.first_name }} instead of just {{ user }}. If you need to store some additional information about users you can also define Profiles where you can implement your own __str__ method.
You will have to implement custom authentication backend that used first_name as username.
During registration, you can duplicate username with first_name or generate random username which you will never use, as you will always use first_name instead.
You will have have to take care of
Take create while creating/registering user.
Username (in your case first name) should be unique
The code in authentication backend would be something like (this is just a sample code):
def authenticate(self, username=None, password=None):
try:
user = User.objects.get(first_name=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
Refer Writing authentication backend.
There are quite a few examples of how to user email to authenticate that you can refer to authenticate using first_name.