初始数据夹具中的用户
默认情况下,我在 fixtures/initial_data.json
中创建一些用户,以便进行一些测试“主题”。我遇到的问题是密码生成。我可以在“字段”中设置密码,但这不会生成散列密码:
[
{ "model": "auth.user",
"pk": 1,
"fields": {
"username": "user1",
"password": "password"
}
}
]
我需要一种生成用户密码的方法。我是否必须像 Django 那样手动执行此操作并生成像 {hash_method}${salt}${hashed_password}
这样的字符串?
I'm creating a few users by default in my fixtures/initial_data.json
so as to have some testing "subjects." The problem I'm experiencing is password generation. I could set the password in the 'fields', but that won't generate a hashed password:
[
{ "model": "auth.user",
"pk": 1,
"fields": {
"username": "user1",
"password": "password"
}
}
]
I need a way to generate the user's password. Do I have to do this manually and generate a string like {hash_method}${salt}${hashed_password}
like Django does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在这种情况下(如果您只需要几个用户)可能更容易的是通过管理员创建一些假用户帐户(包括密码),然后使用 dumpdata 将用户转储到固定文件中:
它将自动为您创建固定装置,并可以稍后与 loaddata 一起使用
(如果您需要大量测试用户,您可以只创建一个假帐户并在其余固定装置中使用散列)
https://docs.djangoproject.com /en/dev/ref/django-admin/#dumpdata-appname-appname-appname-model
What might be easier in this case (and if you only need a few users) is to create some fake user accounts through the admin (including passwords) and then dump the users to a fixtures file using dumpdata:
which will automatically create the fixtures for you and can be used later with loaddata
(You could just create one fake account and use the hash in the rest of your fixtures if you needed lots of test users)
https://docs.djangoproject.com/en/dev/ref/django-admin/#dumpdata-appname-appname-appname-model
好吧,我同意答案,但让我回答原来的问题。
如何获取“Django 会对其进行哈希处理”的密码?
让我们看一下文件
django/contrib/auth/hashers.py
:请参阅此示例会话:
您还可以手动使用哈希器的
encode
方法,但是上面的实用程序函数有您以更简单的方式进行了介绍。OK, I agree with the answers, but let me answer the original questions.
How to get the password "as Django would have hashed it"?
Let's look a the file
django/contrib/auth/hashers.py
:See this example session:
You can also manually use the hasher's
encode
method, but the above utility function has you covered in an easier way.我在编写测试装置时遇到了同样的问题。这是我在单元测试中处理这个问题的方法。
从管理员创建数据并将其转储到固定装置中是可行的,但我不太喜欢依赖于手动执行此操作。所以这就是我所做的 -
就像您所做的那样创建固定装置,然后在
setUp
方法中为用户设置set_password
。user_fixture.json
test_user.py
这样我就可以干净地将密码写在fixture中,并在需要时引用它们,并且只需编辑fixture文件即可创建更多fixture。
I came across the same problem while writing fixtures for tests. Here's how I'm handling this in unit tests.
Creating data from admin and dumping them into fixture works but I don't quite like being dependent on manually doing it. So here's what I do -
Create the fixture just as you've done and then in the
setUp
method,set_password
s for the users.user_fixture.json
test_user.py
This way I can cleanly write the passwords in fixture and refer back to them when I need to and create more fixtures simply by editing the fixture file.
添加到 @GauravButola 的答案中,我创建了一个自定义管理命令来加载夹具并一步修复密码。当不使用测试框架来设置密码时,这非常有用。该示例适用于 django 1.11 以及可能更早的版本。
在提供固定装置的应用程序中添加您的管理命令(这是 python3 次,所以我省略了 init pys):
initdata.py 如下所示:
现在您可以通过调用以下命令加载初始数据并拥有有效的密码:
Adding to the answer of @GauravButola, I created a custom management command to load the fixture and fix the passwords in one step. This is useful when not using a testing framework to do the setup of the passwords. The example is working with django 1.11 and probably earlier versions.
In your app providing the fixture add your management command (this is python3 times, so I omitted the init pys):
With initdata.py looking like this:
Now you can load your initial_data and have valid passwords by calling:
从数据库转储用户信息:
导入/加载特定 JSON 夹具的数据:
Dump Users information from DataBase:
Import / load data especific JSON fixture:
使用方法
make_password(admin_password)
。它位于 django.contrib.auth.hashers 中
Use the method
make_password(admin_password)
.It's in
django.contrib.auth.hashers
很肯定你会的。应该没那么难。最简单的方法是查看我认为的 user.set_password 函数,看看他们是如何做到的。您可以从 python 终端调用该函数,然后将其复制到您的初始数据中!
pretty sure you do. it shouldn't be that difficult. easiest way would be to look at the function
user.set_password
i think it is, and look how they do it. you can probably call the function from a python terminal and then copy it into your initial data!