用户初始数据夹具 [英] Users in initial data fixture

查看:89
本文介绍了用户初始数据夹具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 fixtures / initial_data.json 中默认创建了几个用户,以便有一些测试主题。我遇到的问题是密码生成。我可以在字段中设置密码,但不会生成散列密码:

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"
        }
    }
]

我需要一个生成用户密码的方式。我必须手动执行此操作,像Django那样生成一个像 {hash_method} $ {salt} $ {hashed_pa​​ssword} 的字符串?

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?

推荐答案

在这种情况下可能会更容易(如果您只需要几个用户),就是创建一些假的用户帐户管理员(包括密码),然后使用 dumpdata 将用户转储到夹具文件:

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:

$ python manage.py dumpdata auth.User --indent 4 > users.json

它将为您自动创建灯具,并可在稍后使用loadData

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

这篇关于用户初始数据夹具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆