I am creating an initializing file for for my django project database. I am doing this using a file called initial_data.json
which i have created. For example the following code (when syncdb
is run) creates in the model Word
a new row where name="apple"
:
我正在为我的django项目数据库创建一个初始化文件。我正在使用我创建的名为initial_data.json的文件来完成此操作。例如,以下代码(运行syncdb时)在模型Word中创建一个新行,其中name =“apple”:
[ { "model": "sites.word", "pk": 1, "fields": { "name": "apple" } } ]
[{“model”:“sites.word”,“pk”:1,“fields”:{“name”:“apple”}}]
I have managed to this so far for several models, the problem is with models that have a many-to-many field. I've looked around for the correct way to do this and have come up empty.
到目前为止,我已经为几个模型设法了,问题在于具有多对多字段的模型。我四处寻找正确的方法,并且空洞。
So, for example, if a Model
mood has many Interests
how would I write in the Json file that mood-1's interests are interest-1, interest-2 and interest-3.
那么,例如,如果一个模型情绪有很多兴趣,我怎么写在Json文件中,情绪-1的兴趣是兴趣-1,兴趣-2和兴趣-3。
What is the proper way to write in Json a models many-to-many relation?
在Json中编写多对多模型的正确方法是什么?
EDIT:
编辑:
@pastylegs solution was correct, I was just having trouble because the numbering of my interests was off in the Json file so it couldn't match them with there moods.
@pastylegs解决方案是正确的,我只是遇到了麻烦,因为我的兴趣编号在Json文件中已经关闭,所以它无法与那些情绪相匹配。
2 个解决方案
#1
13
I'm pretty sure the manytomany field of your model can be written like a simple list:
我很确定你的模型的多个字段可以写成一个简单的列表:
[
{
"model": "sites.word",
"pk": 1,
"fields": {
"name": "apple",
"my_m2m_field_name": [1,2,3],
}
}
]
where 1, 2, 3 are the primary keys for the relations
其中1,2,3是关系的主键
#2
5
what I like to do is use the dumpdata command. I fire up a test site, use the admin form or the app itself to add just the data that I want to use in my fixture, then I run
我喜欢做的是使用dumpdata命令。我启动一个测试站点,使用管理表单或应用程序本身添加我想要在我的夹具中使用的数据,然后我运行
./manage.py dumpdata appname > appname/fixtures/initial_data.json
You can dump all the apps together if you leave out appname but I like to do it separately for each model.
如果省略appname,可以将所有应用程序一起转储,但我喜欢为每个模型单独执行。
If you're using 1.3 (I'm not yet) then you can use --exclude
to not dump some parts. I've aslo seen there is a --indent
option to make the output pretty (just found that now while answering your question).
如果您使用的是1.3(我还没有)那么您可以使用--exclude来不转储某些部分。我已经看到有一个--inten选项使输出漂亮(刚刚发现,现在回答你的问题)。
It's one of those things that's easy to miss in the documentation. ;-)
这是文档中很容易遗漏的内容之一。 ;-)
#1
13
I'm pretty sure the manytomany field of your model can be written like a simple list:
我很确定你的模型的多个字段可以写成一个简单的列表:
[
{
"model": "sites.word",
"pk": 1,
"fields": {
"name": "apple",
"my_m2m_field_name": [1,2,3],
}
}
]
where 1, 2, 3 are the primary keys for the relations
其中1,2,3是关系的主键
#2
5
what I like to do is use the dumpdata command. I fire up a test site, use the admin form or the app itself to add just the data that I want to use in my fixture, then I run
我喜欢做的是使用dumpdata命令。我启动一个测试站点,使用管理表单或应用程序本身添加我想要在我的夹具中使用的数据,然后我运行
./manage.py dumpdata appname > appname/fixtures/initial_data.json
You can dump all the apps together if you leave out appname but I like to do it separately for each model.
如果省略appname,可以将所有应用程序一起转储,但我喜欢为每个模型单独执行。
If you're using 1.3 (I'm not yet) then you can use --exclude
to not dump some parts. I've aslo seen there is a --indent
option to make the output pretty (just found that now while answering your question).
如果您使用的是1.3(我还没有)那么您可以使用--exclude来不转储某些部分。我已经看到有一个--inten选项使输出漂亮(刚刚发现,现在回答你的问题)。
It's one of those things that's easy to miss in the documentation. ;-)
这是文档中很容易遗漏的内容之一。 ;-)