在Django中同时创建ManyToMany实例

时间:2018-10-26 17:19:59

标签: python json django django-rest-framework

我在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"
   }

我正在尝试建立模型来代表所有这些信息。我有一个“国家/地区”模型,然后是一个通过ManyToManyField()链接到“国家/地区”的语言,翻译,区域性区域和货币的模型。我正在尝试使用Django Shell将所有国家/地区一次添加到我的数据库中。在内部,我正在运行以下命令:

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()

但是我收到错误消息“禁止直接分配给多对多集合的前端。请改为使用currency.set()。”

将国家/地区实例设置到数据库中时,是否仍然可以设置这4个多对多实例?我只有一个包含所有国家/地区的JSON文件,因此在创建国家/地区实例之前,将每种货币,语言等分开是一项繁重的工作。

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)

1 个答案:

答案 0 :(得分:0)

不,你不能。

ManyToManyField基本上是一个表,其中包含来自两个对象的两个主键。当两端都没有引用时,将很难添加任何关系。结果,ManyToManyField首先需要将真实对象作为参考。