Django Auth User Model Set different field name for password - django

I recently look into Django framework and plan to migrate my old system into it. Therefore, there is legacy mysql database that I need to follow. Is there anyway to change the field name of password of Django User Model? such as "pwd" or "password2".
I got research the Django document, only able to find out changing the username field
https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD

I think you can create a new class that inherits from user model and set the password field to be whatever you like.
Something like:
class MyUser(AbstractBaseUser):
...
password = models.CharField(max_length=100, db_column='custom_name')
...

Related

How I can Add an extra 'status' field in auth_user Table in Django?

I want to add 'status' field in my django auth_user table, Please let me know how i can add this field. I was trying to add by signup form but i am unable to migrate from there, I am getting error.
Is there are any other option where i can add this field in Django default login functionality.
hey you will have to overide the you model.You can add multiple new column
class User(AbstractBaseUser, PermissionsMixin, BaseModelMixin):
status = models.CharField(max_length=12)
and setting file you have to add the
AUTH_USER_MODEL = "accounts.User"
You can inherith a model from AbstractBaseUser class. It provides the core implementation of a user model, including hashed passwords and tokenized password resets. According to the official documentation: https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#specifying-a-custom-user-model
class MyUser(AbstractBaseUser):
status= models.CharField(max_length=40)
...

extending default User model in Django

I've written my first application Django 2.0.
Everything is working fine and the application is almost ready when I realized to replace id primary key field from default integer type to UUID to make database entry more secure.
When I searched for this how to change id of user table to UUID I got many tutorials extending AbstractBaseUser.
Here is I have written own User model.
account/models.py
class User(AbstractBaseUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
But I'm confused more with examples on different sources.
Every example is adding few more fields in extended model like
first_name
last_name
is_staff
is_admin
active
and functions as
def get_fullname(self):
def get_shortname(self):
etc.
I think all these fields and functions are there by default in AUTH_USER_MODEL.
Does extending AbstractBaseUser overwrites AUTH_USER_MODEL and it is required to add all fields which is there by default?
also, I'm using settings.AUTH_USER_MODEL as foreign key in different models. Should It be replaced by account.User model?
I'm also using django-allauth plugin to enable login using the social network and use email only for authentication. Do I require to add email field in the extended model with unique=True?
Django AbstractBaseUser provides only following fields: password, last_login, is_active. So if you are using custom User model inherited from AbstractBaseUser you need to define all other fields such as email manually.
As another part of question just adding AUTH_USER_MODEL = 'users.User' to your settings.py file should make everything works without replace code in your project.
UPD
If you need field like first_name, last_name, etc. to be includet to the model you can use AbstractUser instead of AbstractBaseUser.
As the Django documentation indicates, it's difficult to extend the User table after-the-fact, and not recommended at all for apps. A better way is to create an auxiliary table which has a 1:1 relationship with the user-id. Leave Django's user-table alone and just use this other table to pony-up to it.
The "Django Annoying" project, at https://github.com/skorokithakis/django-annoying#autoonetoonefield, has some very useful "juice" to make this much easier: an AutoOneToOneField. Whereas Django's foreign-key field will throw an error if an record doesn't exist, this field will automagically create one on-the-fly, thereby side-stepping the entire issue. (The documentation page linked-to above shows exactly how this is done.)

Should I use the default users model in django 1.11 for my project

Actually, I'm working on a project where I need to save some details like name, username, password, age, gender etc of every user.
In that website, any user can login to their account, edit information.
So should I use the default users model or create a new model
I suggest you subclass AbstractUser. This option is suitable if you're fine with Django's User fields, but need extra fields. Django documentation also recommends to do this anyway.
If you’re starting a new project, it’s highly recommended to set up a
custom user model, even if the default User model is sufficient for
you. This model behaves identically to the default user model, but
you’ll be able to customize it in the future if the need arises:
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass or additional fields here ...
You also have to point to this model before creating or running any migration in the settings:
AUTH_USER_MODEL = 'yourapp.User'
Default user model in Django save some limited fields about one user. fields are
first_name, last_name, email, password, groups, user_permissions, is_staff, is_active, is_superuser, last_login, date_joined
If you want to save other information of user like birthday, expertise, gender you have to write userprofile model which must be linked one to one with user.
Example:
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OnetoOneField(User)
birthday = models.DateTimeField(default=datetime.datetime.now)
skills = models.CharField(max_length=128)
Actually, If you want to create a custom user model for user, It's mentioned in the Django's Official documentation. For achieving that you have to first inherit your custom user model with AbstractUser class and then pointing that custom user in your settings file by mentioning
AUTH_USER_MODEL = 'mycustom_user_app.MyCustomUser'.
Now django internally knows which model is the User model for the project, and you can access all model managers(e.g. create_user, etc. ) for your custom user. In that way you can use the current models fields and also can add more fields into it. That's the legit way to go with and to customize your User Model as mentioned in the documentation

Django foreign key in registration model

I am using Django 1.7.
I need to develop a user registration model in which at the time of registration, user will have to enter an employer also along with username, email and password. The employer name should be there in the database. Besides the custom user model, I am using another model named employer.
What would be the best way to implement this through a custom registration?
the cleanest way, using OneToOneField(User)
class Employee(models.Model):
user = models.OneToOneField(User)
//here goes your others employee attributes
So when you create your user model,also create its employee instance.

PasswordField in Django model?

I am trying to create a simple model called Username like this:
class User(models.Model):
username = models.CharField(max_length=100) #Id is automatically generated by Django
password = models.CharField(max_length=100)
This is the Django model that I am trying to create. The problem is username and password attribute is stored as CharField whereas I want password to be stored as ** or encrypted form in the database.It seems like they don't have PasswordField like CharField in Django. What's the best way to do it?
as per THE DOCS it's
password = forms.CharField(
widget=forms.PasswordInput(render_value=False),
label="Your Password"
)
and this lends to a
<input type="password" />
in your rendered form
About your storing part, you will need to store an hash of the password, not a list of * or you won't be able to retrieve it anyway. You could use the hashlib module
user.password = hashlib.sha224(user.password).hexdigest()
user.save()
of course you have to pay big attention when implementing this. This above is just a quick example, check the docs for further learning
Django comes with User model. It's under django.contrib.auth.models. The model has everything you need and it's would be silly to start creating your own if there is one already. You also have a user creating and authentication forms in django.contrib.auth.forms, things like set_password method in user model and heaps more stuff.