Issue
I am creating application in Django and now I want create table with username and password. Right now, we generating password with mysql function SHA2(‘password’,256) and my question… is possible use it the same in Django model? I mean everything what will be save to password row, will be saved as hash sha2 in admin app or I must create functions?
this is my models.py
class Users(models.Model):
username = models.CharField(max_length=30)
password = models.CharField(max_length=128)
status = models.IntegerField()
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
online = models.IntegerField()
ip = models.CharField(max_length=20)
created = models.DateTimeField()
def __str__(self):
return f'{self.username}'
Solution
You don’t need to create User
model. You can use this instead. from django.contrib.auth.models import User
.
then you can add additional field into model if you want
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.PROTECT)
valid_from = models.DateTimeField()
valid_to = models.DateTimeField()
online = models.IntegerField()
ip = models.CharField(max_length=20)
created = models.DateTimeField()
You can read it in details in Documentation here.
Answered By – Hemal Patel
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0