Python Django 如何在 django 中创建哈希字段 models.py
Python Django How to Create Hash Fields inside django models.py
我正在开发一个 Python(3.6) & Django(1.10) 项目,我需要在其中保存一些第三方服务的用户凭证,例如用户名、密码和电子邮件,我是仅实施休息 API,因此根本没有 form.py。
那么,如何在 models.py 文件中创建哈希字段?
这是我目前的 models.py:
class DeploymentOnUserModel(models.Model):
deployment_name = models.CharField(max_length=256, )
credentials = models.TextField(blank=False)
project_name = models.CharField(max_length=150, blank=False)
project_id = models.CharField(max_length=150, blank=True)
cluster_name = models.CharField(max_length=256, blank=False)
zone_region = models.CharField(max_length=150, blank=False)
services = models.CharField(max_length=150, choices=services)
configuration = models.TextField(blank=False)
routing = models.TextField(blank=True)
def save(self, **kwargs):
if not self.id and self.services == 'Multiple' and not self.routing:
raise ValidationError("You must have to provide routing for multiple services deployment.")
super().save(**kwargs)
我想添加三个新的哈希字段,例如 用户名、密码 和 电子邮件。
请帮帮我!
提前致谢!
您可以使用标准 CharField
。要存储哈希值,请在保存前使用 make_password
方法:
from django.contrib.auth.hashers import make_password
password = models.CharField(max_length=256)
def save(self, **kwargs):
some_salt = 'some_salt'
password = make_password(self.password, some_salt)
if not self.id and self.services == 'Multiple' and not self.routing:
raise ValidationError("You must have to provide routing for multiple services deployment.")
super().save(**kwargs)
我正在开发一个 Python(3.6) & Django(1.10) 项目,我需要在其中保存一些第三方服务的用户凭证,例如用户名、密码和电子邮件,我是仅实施休息 API,因此根本没有 form.py。 那么,如何在 models.py 文件中创建哈希字段?
这是我目前的 models.py:
class DeploymentOnUserModel(models.Model):
deployment_name = models.CharField(max_length=256, )
credentials = models.TextField(blank=False)
project_name = models.CharField(max_length=150, blank=False)
project_id = models.CharField(max_length=150, blank=True)
cluster_name = models.CharField(max_length=256, blank=False)
zone_region = models.CharField(max_length=150, blank=False)
services = models.CharField(max_length=150, choices=services)
configuration = models.TextField(blank=False)
routing = models.TextField(blank=True)
def save(self, **kwargs):
if not self.id and self.services == 'Multiple' and not self.routing:
raise ValidationError("You must have to provide routing for multiple services deployment.")
super().save(**kwargs)
我想添加三个新的哈希字段,例如 用户名、密码 和 电子邮件。
请帮帮我!
提前致谢!
您可以使用标准 CharField
。要存储哈希值,请在保存前使用 make_password
方法:
from django.contrib.auth.hashers import make_password
password = models.CharField(max_length=256)
def save(self, **kwargs):
some_salt = 'some_salt'
password = make_password(self.password, some_salt)
if not self.id and self.services == 'Multiple' and not self.routing:
raise ValidationError("You must have to provide routing for multiple services deployment.")
super().save(**kwargs)