我默认在我的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?
最满意答案
在这种情况下可能更容易(如果只需要几个用户)是通过管理员(包括密码)创建一些假的用户帐户,然后使用dumpdata将用户转储到fixtures文件:
$ python manage.py dumpdata auth.User --indent 4 > users.json这将为您自动创建灯具,并可在稍后的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:
$ python manage.py dumpdata auth.User --indent 4 > users.jsonwhich 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
更多推荐
发布评论