如何在 Django 中为用户模型加载 sql 夹具?
有谁知道如何使用 sql 夹具加载 auth.User 的初始数据? 对于我的模型,我只需要有一个 < modelname >.sql 文件位于名为 sql 的文件夹中,syncdb 完美地完成了它的工作。但我不知道如何为 auth.User 模型做到这一点。我用谷歌搜索过,但没有成功。
提前致谢,
奥尔多
Does anyone knows how to load initial data for auth.User using sql fixtures?
For my models, I just got have a < modelname >.sql file in a folder named sql that syncdb does it's job beautifully. But I have no clue how to do it for the auth.User model. I've googled it, but with no success.
Thanks in advance,
Aldo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于 SQL 固定装置,您必须专门为
auth
表添加插入语句。您可以使用命令python manage.py sql auth
找到 auth 表的架构。更简单且独立于数据库的方法(除非您想要运行一些额外的 SQL 魔法),就是制作 JSON 或 YAML fixture 文件位于应用程序的装置目录中,其中包含如下数据:
您可以在 django 中快速生成哈希密码shell
每当您运行
syncdb
时都会加载它。For SQL fixtures, you'd have to specifically have insert statements for the
auth
tables. You can find the schema of the auth tables with the commandpython manage.py sql auth
.The much easier and database-independent way (unless you have some additional SQL magic you want to run), is to just make a JSON or YAML fixture file in the fixtures directory of your app with data like this:
You can generate a hashed password quickly in a django shell
This will get loaded whenever you run
syncdb
.您正在寻找
loaddata
:但我认为该命令只能处理 XML、YAML、Python 或 JSON 格式的文件(参见此处)。要创建此类适当的文件,请查看
dumpdata
方法。You are looking for
loaddata
:But I think the command can only deal with files in XML, YAML, Python or JSON format (see here). To create such appropriate files, have a look at the
dumpdata
method.感谢您的回答。我找到了适合我的解决方案,巧合的是布莱恩的建议之一。首先,
我断开了在syncdb之后创建超级用户的信号,因为我的超级用户在我的auth_user固定装置中:
models.py:
然后我创建了一个在syncdb之后调用的信号:
<强><我的项目>>/< myapp >/management/__init__.py
在我的 settings.py 中,我添加了 FIXTURES_FILE 以及带有 sql 转储的 .sql 文件的路径。
我还没有找到的一件事是如何仅在创建表后触发此信号,而不是每次触发syncdb 时。解决这个问题的临时方法是在我的 sql 命令中使用 INSERT IGNORE INTO 。
我知道这个解决方案远非完美,非常欢迎批评/改进/意见!
问候,
奥尔多
Thanks for your answers. I've found the solution that works for me, and for coincidence was one of Brian's suggestion. Here it is:
Firs I disconnected the signal that created the Super User after syncdb, for I have my super user in my auth_user fixture:
models.py:
Then I created a signal to be called after syncdb:
< myproject >/< myapp >/management/__init__.py
And in my settings.py I added FIXTURES_FILE with the path to my .sql file with the sql dump.
One thing that I still haven't found is how to fire this signal only after the tables are created, and not everytime syncdb is fired. A temporary work around for this is use INSERT IGNORE INTO in my sql command.
I know this solution is far from perfect, and critics/improvements/opinions are very welcome!
Regards,
Aldo
有一个技巧:(在 Django 1.3.1 上测试)
解决方案:
python manage.py startapp auth_fix
mkdir auth_fix/fixtures
python manage.py dumpdata auth > auth_fixtures/fixtures/initial_data.json
在settings.py内的INSTALLED_APPS中包含auth_fix
下次运行
python manage.pysyncdb
时,Django将自动加载auth固定装置。说明:
此示例展示了如何在 JSON 中执行此操作,但您基本上可以使用您选择的格式。
There is a trick for this: (tested on Django 1.3.1)
Solution:
python manage.py startapp auth_fix
mkdir auth_fix/fixtures
python manage.py dumpdata auth > auth_fixtures/fixtures/initial_data.json
Include auth_fix in INSTALLED_APPS inside settings.py
Next time you run
python manage.py syncdb
, Django will load the auth fixture automatically.Explanation:
python manage.py dumpdata auth
will dump the "auth" data in the DB with all the Groups and Users information. The rest of the command simply redirects the output into a file called "initial_data.json" which is the one that Django looks for when you run "syncdb".This example shows how to do it in JSON but you can basically use the format of your choice.
如果您碰巧正在使用South进行数据库迁移,那么创建用户非常简单。
首先,创建裸数据迁移。它需要包含在某些应用程序中。如果您有一个放置共享代码的通用应用程序,那将是一个不错的选择。如果您有一个集中用户相关代码的应用程序,那就更好了。
相关的迁移代码可能如下所示:
然后只需运行迁移即可插入身份验证用户。
If you happen to be doing database migrations with south, creating users is very simple.
First, create a bare data migration. It needs to be included in some application. If you have a common app where you place shared code, that would be a good choice. If you have an app where you concentrate user-related code, that would be even better.
The pertinent migration code might look something like this:
Then simply run the migration and the auth users should be inserted.
一个选项是手动导入 auth.User SQL,然后将其转储到标准 Django 固定装置(如果您希望syncdb 找到它,请将其命名为initial_data)。您通常可以将此文件放入任何应用程序的固定装置目录中,因为固定数据都将使用正确的应用程序标签进行键入。或者您可以创建一个空/虚拟应用程序并将其放置在那里。
另一种选择是覆盖syncdb命令并以您认为合适的方式应用固定装置。
我同意 Felix 的观点,Django 中不存在用于使用 SQL 填充 contrib 应用程序的重要自然钩子。
An option is to import your auth.User SQL manually and subsequently dump it out to a standard Django fixture (name it initial_data if you want syncdb to find it). You can generally put this file into any app's fixtures dir since the fixtured data will all be keyed with the proper app_label. Or you can create an empty/dummy app and place it there.
Another option is to override the syncdb command and apply the fixture in a manner as you see fit.
I concur with Felix that there is no non-trivial natural hook in Django for populating contrib apps with SQL.
我只是将 SQL 语句添加到另一个模型的自定义 sql 文件中。我选择了 Employee 模型,因为它取决于 auth_user。
我编写的自定义 SQL 实际上从我的遗留应用程序中读取并从中提取用户信息,并使用 REPLACE 而不是 INSERT(我使用的是 MySQL),因此我可以随时运行它。
我将 REPLACE...SELECT 语句放在一个过程中,以便可以轻松手动运行或使用 cron 计划运行。
I simply added SQL statements into the custom sql file for another model. I chose my Employee model because it depends on auth_user.
The custom SQL I wrote actually reads from my legacy application and pulls user info from it, and uses REPLACE rather than INSERT (I'm using MySQL) so I can run it whenever I want.
And I put that REPLACE...SELECT statement in a procedure so that it's easy to run manually or scheduled with cron.