从 json 文件映射时,peewee 模型的正确定义是什么?

What's the right definition of peewee model while mapping from json file?

嗨堆栈溢出社区!这是我在这里的第一个问题,但我试图事先找到答案。现在我正在使用 json 文件 like so 加载数据(我实际上有一个名为 persons.json,而不是 API 的 json 文件) =14=] 到 SQLite 数据库。如您所见,json 文件有多个嵌套的字典。我的peewee模型如下:

import json
import sqlite3
from peewee import *

db = SqliteDatabase('persons.sqlite3')

class Person(Model):
    gender = CharField()
    name = CharField()
    location = CharField()
    email = CharField()
    login = CharField()
    dob = CharField()
    registered = CharField()
    phone = CharField()
    cell = CharField()
    id_ = CharField()
    picture = CharField()
    nat = CharField()
    count_dob = IntegerField()

这就是我将 json 文件中的所有数据加载到 SQLite 数据库的方式:

db.connect()

db.create_tables([Person])

with open('persons.json', encoding='utf8') as persons:
    persons_data = json.load(persons)


for person in persons_data['results']:
    p = Person(gender=person['gender'], name=person['name'], location=person['location'], email=person['email'], 
                login=person['login'], dob=person['dob'], registered=person['registered'], phone=person['phone'],
                cell=person['cell'], id_=person['id'], picture=person['picture'], nat=person['nat'])

我的问题是,您认为我的模型中的变量是否被正确定义(基本上每个变量都作为 CharField)?问题是,稍后每当我查询数据库并需要访问其中一些嵌套字典时,它们实际上是一个字符串,我可以使用 ast.literal_eval 将其转换回字典,但我不认为看起来很好。我想到了一个解决方案 - 对于我模型中的所有 'dictionary type' 变量('location'、'dob' 等),而不是使用 CharField() 我可能会使用 JSONField() - 虽然不知道该怎么做。你能就那个提出建议吗?

关系数据库不支持“嵌套”。这是基础。嵌套的任何内容都应该在单独的 table 中或作为 flat/scalar 值在其自己的列中。

https://en.wikipedia.org/wiki/Database_normalization