初始数据夹具中的用户(Users in initial data fixture)

编程入门 行业动态 更新时间:2024-10-27 05:27:46
初始数据夹具中的用户(Users in initial data fixture)

我默认在我的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.json

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

更多推荐

本文发布于:2023-07-22 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:夹具   数据   用户   Users   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!