Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django set_密码无属性_Python_Django_Sqlite - Fatal编程技术网

Python Django set_密码无属性

Python Django set_密码无属性,python,django,sqlite,Python,Django,Sqlite,我正在尝试使用set_password()函数,但出现以下错误 'Member' object has no attribute 'set_password' 当我使用它时会出现。如果我取出set_password()函数,密码将存储在数据库中,但不会被散列 view.py user = Member(username=u, password=p, email=e, security=s) user.set_password(p) user.save() 型号.py class Member

我正在尝试使用set_password()函数,但出现以下错误

'Member' object has no attribute 'set_password'
当我使用它时会出现。如果我取出set_password()函数,密码将存储在数据库中,但不会被散列

view.py

user = Member(username=u, password=p, email=e, security=s)
user.set_password(p)
user.save()
型号.py

class Member(models.Model):
    username = models.CharField(max_length=16,primary_key=True)
    password = models.CharField(max_length=16)
    email = models.CharField(max_length=325)
    security = models.CharField(max_length=16)
    profile = models.OneToOneField(Profile, null=True)
    following = models.ManyToManyField("self", symmetrical=False)
    from_member_id = models.CharField(max_length=16)

    def __str__(self):
        return self.username

models.Model
不会自动提供设置密码功能。
您必须自己定义它,或者从django Usermodel中派生
成员
,正如错误消息告诉您的那样,没有定义方法
设置密码
。 您可以自己实现它,或者(更好)通过子类化django的
abrstBaseUser来创建您的
成员
模型:

class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=16,primary_key=True)
    password = models.CharField(max_length=16)
    email = models.CharField(max_length=325)
    security = models.CharField(max_length=16)
    profile = models.OneToOneField(Profile, null=True)
    following = models.ManyToManyField("self", symmetrical=False)
    from_member_id = models.CharField(max_length=16)

    USERNAME_FIELD = 'username'
您可以在

中找到有关自定义用户模型的更多信息。有关的文档非常清晰和全面。除此之外,您的模型必须是AbstractBaseUser的子类,它提供了
set\u password
方法


还请注意,16个字符的长度几乎不足以存储散列、盐渍密码。

不确定您是否这样做,但使用OneToOneField(用户)创建模型并为其提供其他字段可能更容易。您只需要记住保存在新字段中,否则在您呼叫时字段将不会显示。 设置
user\u form=Member(request.POST)

set\u password()
user = user_form.save()
user.set_password(user.password)
profile = user.userprofile
profile.bio = request.POST['bio']
profile.save()