将嵌套的dict / json转换为django ORM模型,无需对数据结构进行硬编码

时间:2017-05-01 10:39:17

标签: python json django dictionary

我想将json文件中的数据导入到我的django db中。 json包含嵌套对象。

目前的步骤是:

  1. 设置我的django对象模型以匹配json架构(手动完成 - 请参阅下面的models.py文件)
  2. 使用mydict = json.loads(file.read())(已完成)
  3. 将json文件导入python dict
  4. 将dict转换为django模型(已完成 - 但解决方案并不漂亮)
  5. 有没有办法可以将我的嵌套dict转换为django模型(即步骤3),而无需将数据结构硬编码到逻辑中?

    基于示例json文件自动生成django模型(即models.py文件)的加分点。

    提前致谢!

    我目前是怎么做的

    如果dict不包含任何嵌套dicts,则步骤3很简单 - 只需construct a new object from the dict,即MyModel.objects.create(**mydict)use django fixtures

    但是,因为我的json / dict包含嵌套对象,所以我现在正在执行第3步:

    # read the json file into a python dict
    d = json.loads(myfile.read())
    
    # construct top-level object using the top-level dict
    # (excluding nested lists of dicts called 'judges' and 'contestants')
    c = Contest.objects.create(**{k:v for k,v in d.items() if k not in ('judges', 'contestants')})
    
    # construct nested objects using the nested dicts
    for judge in d['judges']:
        c.judge_set.create(**judge)
    for contestant in d['contestants']:
        ct = c.contestant_set.create(**{k:v for k,v in contestant.items() if k not in ('singers', 'songs')})
        # all contestants sing songs
        for song in contestant['songs']:
            ct.song_set.create(**song)
        # not all contestants have a list of singers
        if 'singers' in contestant:
            for singer in contestant['singers']:
                ct.singer_set.create(**singer)
    

    这有效,但需要将数据结构硬编码到逻辑中:

    • 在调用create()时需要硬编码要排除的嵌套dicts的名称(如果您尝试将嵌套的dict传递给create(),则会抛出TypeError)。我想过要**{k:v for k,v in contestant.items() if not hasattr(v, 'pop')}来排除列表和词组,但我怀疑这不会在100%的时间内起作用。
    • 需要硬编码逻辑来迭代创建嵌套对象
    • 需要硬编码逻辑来处理并不总是存在的嵌套对象

    数据结构

    示例json看起来像这样:

    {
      "assoc": "THE BRITISH ASSOCIATION OF BARBERSHOP SINGERS",
      "contest": "QUARTET FINAL (NATIONAL STREAM)",
      "location": "CHELTENHAM",
      "year": "2007/08",
      "date": "25/05/2008",
      "type": "quartet final",
      "filename": "BABS/2008QF.pdf"
      "judges": [
        {"cat": "m", "name": "Rod"},
        {"cat": "m", "name": "Bob"},
        {"cat": "p", "name": "Pat"},
        {"cat": "p", "name": "Bob"},
        {"cat": "s", "name": "Mark"},
        {"cat": "s", "name": "Barry"},
        {"cat": "a", "name": "Phil"}
      ],
      "contestants": [
        {
          "prev_tot_score": "1393",
          "tot_score": "2774",
          "rank_m": "1",
          "rank_s": "1",
          "rank_p": "1",
          "rank": "1", "name": "Monkey Magic",
          "pc_score": "77.1",
          "songs": [
            {"title": "Undecided Medley","m": "234","s": "226","p": "241"},
            {"title": "What Kind Of Fool Am I","m": "232","s": "230","p": "230"},
            {"title": "Previous","m": "465","s": "462","p": "454"}
          ],
          "singers": [
            {"part": "tenor","name": "Alan"},
            {"part": "lead","name": "Zac"},
            {"part": "bari","name": "Joe"},
            {"part": "bass","name": "Duncan"}
          ]
        },
        {
          "prev_tot_score": "1342",
          "tot_score": "2690",
          "rank_m": "2",
          "rank_s": "2",
          "rank_p": "2",
          "rank": "2", "name": "Evolution",
          "pc_score": "74.7",
          "songs": [
            {"title": "It's Impossible","m": "224","s": "225","p": "218"},
            {"title": "Come Fly With Me","m": "225","s": "222","p": "228"},
            {"title": "Previous","m": "448","s": "453","p": "447"}
          ],
          "singers": [
            {"part": "tenor","name": "Tony"},
            {"part": "lead","name": "Michael"},
            {"part": "bari","name": "Geoff"},
            {"part": "bass","name": "Stuart"}
          ]
        },
      ],
    }
    

    我的models.py文件:

    from django.db import models
    
    # Create your models here.
    
    class Contest(models.Model):
        assoc = models.CharField(max_length=100)
        contest = models.CharField(max_length=100)
        date = models.DateField()
        filename = models.CharField(max_length=100)
        location = models.CharField(max_length=100)
        type = models.CharField(max_length=20)
        year = models.CharField(max_length=20)
    
    
    class Judge(models.Model):
        contest = models.ForeignKey(Contest, on_delete=models.CASCADE)
        name = models.CharField(max_length=60)
        cat = models.CharField('Category', max_length=2)
    
    
    class Contestant(models.Model):
        contest = models.ForeignKey(Contest, on_delete=models.CASCADE)
        name = models.CharField(max_length=100)
        tot_score = models.IntegerField('Total Score')
        rank_m = models.IntegerField()
        rank_s = models.IntegerField()
        rank_p = models.IntegerField()
        rank = models.IntegerField()
        pc_score = models.DecimalField(max_digits=4, decimal_places=1)
        # optional fields
        director = models.CharField(max_length=100, blank=True, null=True)
        size = models.IntegerField(blank=True, null=True)
        prev_tot_score = models.IntegerField(blank=True, null=True)
    
    
    class Song(models.Model):
        contestant = models.ForeignKey(Contestant, on_delete=models.CASCADE)
        title = models.CharField(max_length=100)
        m = models.IntegerField('Music')
        s = models.IntegerField('Singing')
        p = models.IntegerField('Performance')
    
    class Singer(models.Model):
        contestant = models.ForeignKey(Contestant, on_delete=models.CASCADE)
        name = models.CharField(max_length=100)
        part = models.CharField('Category', max_length=5)
    

1 个答案:

答案 0 :(得分:1)

您可以递归浏览json对象并使用键映射类来动态实例化模型。这是一个想法(不是一个有效的解决方案!):

 key_model = {
        "contestants": Contestant,
        "singers": Singer
 }

 def make_sub_model(parent, model, vals):
    for v in vals:
       child = create_model(model, v)
       parent.add_child(child) # or whatever it is with Django Models

def create_model(model, obj):
    # model should be the class and obj a dict

    # take care of the top lvl object
    to_process = [] # store nest models
    parent = {} # store parent attributes
    for k, v in obj.items():
        if isinstance(v, list): # you probably want dict as well
            to_process.append((k, v))
        else:
           parent[k] = v

    parent_obj = model.create(**parent)
    # now process the chidlrend
    for k, v in to_process:
        make_sub_model(parent_obj, key_model[k], v)

    return parent_obj

但最后,我会阻止此,因为您使用的是基于架构的存储(SQL),因此您的代码应该强制输入与您的架构匹配(您无法处理任何内容)不管怎样,在飞行中不同)。如果您根本不关心拥有架构,那就选择No-SQL解决方案并且您不会遇到此问题。或者像PostgresSQL这样的混合体。