[英]Creating ManyToMany instances at the same time in Django
I've got data for all of the countries on a json that looks like this: 我在json上获得了所有国家/地区的数据,如下所示:
{
"name":"Afghanistan",
"topLevelDomain":[
".af"
],
"alpha2Code":"AF",
"alpha3Code":"AFG",
"callingCodes":[
"93"
],
"capital":"Kabul",
"altSpellings":[
"AF",
"Afġānistān"
],
"region":"Asia",
"subregion":"Southern Asia",
"population":27657145,
"latlng":[
33.0,
65.0
],
"demonym":"Afghan",
"area":652230.0,
"gini":27.8,
"timezones":[
"UTC+04:30"
],
"borders":[
"IRN",
"PAK",
"TKM",
"UZB",
"TJK",
"CHN"
],
"nativeName":"افغانستان",
"numericCode":"004",
"currencies":[
{
"code":"AFN",
"name":"Afghan afghani",
"symbol":"؋"
}
],
"languages":[
{
"iso639_1":"ps",
"iso639_2":"pus",
"name":"Pashto",
"nativeName":"پښتو"
},
{
"iso639_1":"uz",
"iso639_2":"uzb",
"name":"Uzbek",
"nativeName":"Oʻzbek"
},
{
"iso639_1":"tk",
"iso639_2":"tuk",
"name":"Turkmen",
"nativeName":"Türkmen"
}
],
"translations":{
"de":"Afghanistan",
"es":"Afganistán",
"fr":"Afghanistan",
"ja":"アフガニスタン",
"it":"Afghanistan",
"br":"Afeganistão",
"pt":"Afeganistão",
"nl":"Afghanistan",
"hr":"Afganistan",
"fa":"افغانستان"
},
"regionalBlocs":[
{
"acronym":"SAARC",
"name":"South Asian Association for Regional Cooperation",
"otherAcronyms":[
],
"otherNames":[
]
}
],
"cioc":"AFG"
}
I'm trying to make models to represent all of this information. 我正在尝试建立模型来代表所有这些信息。 I have one Country model, and then a model for language, translation, regional blocks, and currency that are linked to Country through a ManyToManyField().
我有一个“国家/地区”模型,然后是一个通过ManyToManyField()链接到“国家/地区”的语言,翻译,区域性区域和货币的模型。 I'm trying to add all of the countries at once to my database using the Django shell.
我正在尝试使用Django Shell将所有国家/地区一次添加到我的数据库中。 Inside I'm running this command:
在内部,我正在运行以下命令:
import json
from api.models import Country
from api.models import currencies, languages, translations, regionalBlocs
with open('data.json') as f:
data_json = json.load(f)
for data in data_json:
data = Country(name=data['name'], topLevelDomain=data['topLevelDomain'], callingCodes=data['callingCodes'], capital=data['capital'], altSpellings=data['altSpellings'], region=data['region'], subregion=data['subregion'], population=data['population'],latlng=data['latlng'], demonym=data['demonym'], area=['area'], gini=data['gini'], timezones=data['timezones'], borders=data['borders'], nativeName=data['nativeName'], numericCode=data['numericCode'], currencies=data['currencies'], languages=data['languages'], translations=data['translations'], flag=data['flag'], regionalBlocs=data['regionalBlocs'], cioc=data['cioc'])
data.save()
But I get the error " Direct assignment to the forward side of a many-to-many set is prohibited. Use currencies.set() instead." 但是我收到错误消息“禁止直接分配给多对多集合的前端。请改为使用currency.set()。”
Is there anyway I can set these 4 many-to-many instances while I'm setting my country instances into my database? 将国家/地区实例设置到数据库中时,是否可以设置这4个多对多实例? I only have this one JSON file with all of the countries, so it would be a lot of work to separate out every currency, language, etc before making country instances.
我只有所有国家/地区都有这个JSON文件,因此在制作国家/地区实例之前,要分离出每种货币,语言等,将需要大量工作。
models.py: models.py:
class languages(models.Model):
iso639_1 = models.CharField(max_length=255, null=True)
iso639_2 = models.CharField(max_length=255, null=True)
name = models.CharField(max_length=255, null=True)
nativeName = models.CharField(max_length=255, null=True)
class Country(models.Model):
languages = models.ManyToManyField(languages)
No, you can't. 不,你不能。
ManyToManyField
is basically a table with two primary keys from both objects. ManyToManyField
基本上是一个具有两个对象的两个主键的表。 It would be difficult to add any relationship when there is no reference from both ends. 当两端都没有引用时,将很难添加任何关系。 As a result,
ManyToManyField
would require to have a real object as a reference first. 结果,
ManyToManyField
首先需要将真实对象作为参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.