话不多说直接上代码
from django.contrib.auth.hashersimport make_password
class User(models.Model):
username = models.CharField(max_length=255,verbose_name='用户名')
password = models.CharField(max_length=255, verbose_name='密码')
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
self.password = make_password(self.password)
super().save(force_insert=False,force_update=False,using=None,update_fields=None)
def __str__(self):
return self.username
解释一下,需要在模型类中重写save方法,将加密后的密码save