简体   繁体   English

在Django中同时创建ManyToMany实例

[英]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.

相关问题 在Django ManyToMany关系中存储同一项目的多个实例 - Store multiple instances of same item in Django ManyToMany relationship Django麻烦创建ManyToMany字段 - Django Trouble Creating ManyToMany Field Django Rest 创建嵌套对象 (ManyToMany) - Django Rest creating Nested-Objects (ManyToMany) Django在表单中为许多领域创建模型 - django creating models inside of form for manytomany field Django在多个实例中创建查询集或唯一项列表 - Django create queryset or list of unique items in multiple manytomany instances 在 Django 中创建“评论”实例以进行迁移? - Creating “Comment” instances in Django for migration? 当嵌套序列化程序中有另一个模型(manytomany)时,我如何在 django 模型上发布,我想同时创建两者 - How do I post on a django model when it has another model (manytomany ) inside nested serializers, I want at the same time to create both Django:如何访问以前的 model class 实例,同时创建相同 ZA2F2ED4F8EBC2CBB14C21A2DZ 实例的新实例 - Django: How to access the previous model class instances while creating new instance of the same class? 保存后自动在Django中的ManyToMany字段中创建记录 - Auto creating records across a ManyToMany Field in Django upon save Django为中间表中的字段创建错误类型(manytomany) - Django creating wrong type for fields in intermediate table (manytomany)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM