gpt4 book ai didi

django - 创建 Django 管理员帐户 : null value of password

转载 作者:行者123 更新时间:2023-12-01 08:52:51 24 4
gpt4 key购买 nike

在 Django 项目中,我有以下脚本来创建 admin 帐户:

User.objects.all().delete()
admin = User()
admin.username = 'admin'
admin.password = admin.set_password('admin')
admin.save()

但我收到错误消息:

psycopg2.IntegrityError: null value in column "password" violates not-null constraint

这是为什么呢?

最佳答案

来自 the docs :

set_password(raw_password): Sets the user’s password to the given raw string, taking care of the password hashing.

它为你设置密码,然后返回None,所以不要将它分配回user.password。换句话说,只需这样做:

admin.username = 'admin'
admin.set_password('admin')
admin.save()

关于django - 创建 Django 管理员帐户 : null value of password,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37303744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com