Django:尝试组织 django 装置
我创建了一些模型,我想为其提供初始数据。问题是有几个模型,我想组织数据。
目前,我有一个很大的 JSON 文件:包含数据的 initial_data.json
。我以为我可以使用一些注释,但是 JSON 没有注释!我真的很想使用 json。
所以,该文件就像:
[
{
"model": "app1.Model1",
"pk": 1,
"fields": {
"nombre": "A convenir con el vendedor"
}
},
//many more
{
"model": "app2.Model1",
"pk": 1,
"fields": {
"nombre": "A convenir con el vendedor"
}
},
//many more
{
"model": "app2.Model1",
"pk": 1,
"fields": {
"nombre": "A convenir con el vendedor"
}
},
]
所以,我想我可以将它们组织在不同的文件中,并使用一些初始脚本加载它们。这个想法不是发出几个 python manage.py loaddata thisApp.Model
但是,这样就很难分离出最初不加载的文件。
以下是示例文件:
+app1
+fixtures
model1.json
model2.json
+app2
+fixtures
model1.json
model2.json
+app3
+fixtures
model1.json
model2.json
您知道如何保持简单吗?
I've some models created wich i'd like to provide initial data for. The problem is that there are several models, and i'd like to organize the data.
Currently, i've a big JSON file: initial_data.json
with the data. I was thinking i could use some comments, but JSON has no comments! I really want to use json.
So, the file is like:
[
{
"model": "app1.Model1",
"pk": 1,
"fields": {
"nombre": "A convenir con el vendedor"
}
},
//many more
{
"model": "app2.Model1",
"pk": 1,
"fields": {
"nombre": "A convenir con el vendedor"
}
},
//many more
{
"model": "app2.Model1",
"pk": 1,
"fields": {
"nombre": "A convenir con el vendedor"
}
},
]
So, i thought i could organize them in different files, and with some initial script load them. The idea is not issue several python manage.py loaddata thisApp.Model
But, then it would be difficult to separate the files that are not ment to be loaded at initial time.
Here are the files as example:
+app1
+fixtures
model1.json
model2.json
+app2
+fixtures
model1.json
model2.json
+app3
+fixtures
model1.json
model2.json
Do you have any idea how to keep simple?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像你说的,创建几个文件,并编写一个脚本,将它们组合成
initial_data.json
并调用所需的django.core.management
命令。这就是我所做的。like you said, create several files, and write a script that combines them into
initial_data.json
and invokes the neededdjango.core.management
command. this is what I do.调用包含初始数据的文件“initial_data.json”-syncdb 将仅加载这些文件。您可以使用manage.py loaddata手动加载其他数据。
https://docs.djangoproject。 com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures
Call the files that contain initial data "initial_data.json" - syncdb will only load those. You can load the others manually with manage.py loaddata.
https://docs.djangoproject.com/en/dev/howto/initial-data/#automatically-loading-initial-data-fixtures