• 省市县三级联动数据导入数据库


    中国行政区域三级联动数据导入数据库脚本

    area.py 数据链接github https://github.com/adyliu/china_area/blob/master/area_code_2019.json

    部分数据格式

    area = [{
        "code": 110000000000,
        "name": "北京市",
        "level": 1,
        "pcode": 0,
        "children": [{
            "code": 110100000000,
            "name": "市辖区",
            "level": 2,
            "pcode": 110000000000,
            "children": [{
                "code": 110101000000,
                "name": "东城区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110102000000,
                "name": "西城区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110105000000,
                "name": "朝阳区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110106000000,
                "name": "丰台区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110107000000,
                "name": "石景山区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110108000000,
                "name": "海淀区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110109000000,
                "name": "门头沟区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110111000000,
                "name": "房山区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110112000000,
                "name": "通州区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110113000000,
                "name": "顺义区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110114000000,
                "name": "昌平区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110115000000,
                "name": "大兴区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110116000000,
                "name": "怀柔区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110117000000,
                "name": "平谷区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110118000000,
                "name": "密云区",
                "level": 3,
                "pcode": 110100000000
            }, {
                "code": 110119000000,
                "name": "延庆区",
                "level": 3,
                "pcode": 110100000000
            }]
        }]
    }, {
        "code": 120000000000,
        "name": "天津市",
        "level": 1,
        "pcode": 0,
        "children": [{
            "code": 120100000000,
            "name": "市辖区",
            "level": 2,
            "pcode": 120000000000,
            "children": [{
                "code": 120101000000,
                "name": "和平区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120102000000,
                "name": "河东区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120103000000,
                "name": "河西区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120104000000,
                "name": "南开区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120105000000,
                "name": "河北区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120106000000,
                "name": "红桥区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120110000000,
                "name": "东丽区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120111000000,
                "name": "西青区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120112000000,
                "name": "津南区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120113000000,
                "name": "北辰区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120114000000,
                "name": "武清区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120115000000,
                "name": "宝坻区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120116000000,
                "name": "滨海新区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120117000000,
                "name": "宁河区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120118000000,
                "name": "静海区",
                "level": 3,
                "pcode": 120100000000
            }, {
                "code": 120119000000,
                "name": "蓟州区",
                "level": 3,
                "pcode": 120100000000
            }]
        }]
    }, {
        "code": 130000000000,
        "name": "河北省",
        "level": 1,
        "pcode": 0,
        "children": [{
            "code": 130100000000,
            "name": "石家庄市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130101000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130102000000,
                "name": "长安区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130104000000,
                "name": "桥西区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130105000000,
                "name": "新华区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130107000000,
                "name": "井陉矿区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130108000000,
                "name": "裕华区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130109000000,
                "name": "藁城区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130110000000,
                "name": "鹿泉区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130111000000,
                "name": "栾城区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130121000000,
                "name": "井陉县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130123000000,
                "name": "正定县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130125000000,
                "name": "行唐县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130126000000,
                "name": "灵寿县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130127000000,
                "name": "高邑县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130128000000,
                "name": "深泽县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130129000000,
                "name": "赞皇县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130130000000,
                "name": "无极县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130131000000,
                "name": "平山县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130132000000,
                "name": "元氏县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130133000000,
                "name": "赵县",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130171000000,
                "name": "石家庄高新技术产业开发区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130172000000,
                "name": "石家庄循环化工园区",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130181000000,
                "name": "辛集市",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130183000000,
                "name": "晋州市",
                "level": 3,
                "pcode": 130100000000
            }, {
                "code": 130184000000,
                "name": "新乐市",
                "level": 3,
                "pcode": 130100000000
            }]
        }, {
            "code": 130200000000,
            "name": "唐山市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130201000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130202000000,
                "name": "路南区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130203000000,
                "name": "路北区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130204000000,
                "name": "古冶区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130205000000,
                "name": "开平区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130207000000,
                "name": "丰南区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130208000000,
                "name": "丰润区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130209000000,
                "name": "曹妃甸区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130224000000,
                "name": "滦南县",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130225000000,
                "name": "乐亭县",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130227000000,
                "name": "迁西县",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130229000000,
                "name": "玉田县",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130271000000,
                "name": "唐山市芦台经济技术开发区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130272000000,
                "name": "唐山市汉沽管理区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130273000000,
                "name": "唐山高新技术产业开发区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130274000000,
                "name": "河北唐山海港经济开发区",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130281000000,
                "name": "遵化市",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130283000000,
                "name": "迁安市",
                "level": 3,
                "pcode": 130200000000
            }, {
                "code": 130284000000,
                "name": "滦州市",
                "level": 3,
                "pcode": 130200000000
            }]
        }, {
            "code": 130300000000,
            "name": "秦皇岛市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130301000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130302000000,
                "name": "海港区",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130303000000,
                "name": "山海关区",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130304000000,
                "name": "北戴河区",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130306000000,
                "name": "抚宁区",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130321000000,
                "name": "青龙满族自治县",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130322000000,
                "name": "昌黎县",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130324000000,
                "name": "卢龙县",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130371000000,
                "name": "秦皇岛市经济技术开发区",
                "level": 3,
                "pcode": 130300000000
            }, {
                "code": 130372000000,
                "name": "北戴河新区",
                "level": 3,
                "pcode": 130300000000
            }]
        }, {
            "code": 130400000000,
            "name": "邯郸市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130401000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130402000000,
                "name": "邯山区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130403000000,
                "name": "丛台区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130404000000,
                "name": "复兴区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130406000000,
                "name": "峰峰矿区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130407000000,
                "name": "肥乡区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130408000000,
                "name": "永年区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130423000000,
                "name": "临漳县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130424000000,
                "name": "成安县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130425000000,
                "name": "大名县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130426000000,
                "name": "涉县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130427000000,
                "name": "磁县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130430000000,
                "name": "邱县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130431000000,
                "name": "鸡泽县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130432000000,
                "name": "广平县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130433000000,
                "name": "馆陶县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130434000000,
                "name": "魏县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130435000000,
                "name": "曲周县",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130471000000,
                "name": "邯郸经济技术开发区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130473000000,
                "name": "邯郸冀南新区",
                "level": 3,
                "pcode": 130400000000
            }, {
                "code": 130481000000,
                "name": "武安市",
                "level": 3,
                "pcode": 130400000000
            }]
        }, {
            "code": 130500000000,
            "name": "邢台市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130501000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130502000000,
                "name": "桥东区",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130503000000,
                "name": "桥西区",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130521000000,
                "name": "邢台县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130522000000,
                "name": "临城县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130523000000,
                "name": "内丘县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130524000000,
                "name": "柏乡县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130525000000,
                "name": "隆尧县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130526000000,
                "name": "任县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130527000000,
                "name": "南和县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130528000000,
                "name": "宁晋县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130529000000,
                "name": "巨鹿县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130530000000,
                "name": "新河县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130531000000,
                "name": "广宗县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130532000000,
                "name": "平乡县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130533000000,
                "name": "威县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130534000000,
                "name": "清河县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130535000000,
                "name": "临西县",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130571000000,
                "name": "河北邢台经济开发区",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130581000000,
                "name": "南宫市",
                "level": 3,
                "pcode": 130500000000
            }, {
                "code": 130582000000,
                "name": "沙河市",
                "level": 3,
                "pcode": 130500000000
            }]
        }, {
            "code": 130600000000,
            "name": "保定市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130601000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130602000000,
                "name": "竞秀区",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130606000000,
                "name": "莲池区",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130607000000,
                "name": "满城区",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130608000000,
                "name": "清苑区",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130609000000,
                "name": "徐水区",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130623000000,
                "name": "涞水县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130624000000,
                "name": "阜平县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130626000000,
                "name": "定兴县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130627000000,
                "name": "唐县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130628000000,
                "name": "高阳县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130629000000,
                "name": "容城县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130630000000,
                "name": "涞源县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130631000000,
                "name": "望都县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130632000000,
                "name": "安新县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130633000000,
                "name": "易县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130634000000,
                "name": "曲阳县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130635000000,
                "name": "蠡县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130636000000,
                "name": "顺平县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130637000000,
                "name": "博野县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130638000000,
                "name": "雄县",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130671000000,
                "name": "保定高新技术产业开发区",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130672000000,
                "name": "保定白沟新城",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130681000000,
                "name": "涿州市",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130682000000,
                "name": "定州市",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130683000000,
                "name": "安国市",
                "level": 3,
                "pcode": 130600000000
            }, {
                "code": 130684000000,
                "name": "高碑店市",
                "level": 3,
                "pcode": 130600000000
            }]
        }, {
            "code": 130700000000,
            "name": "张家口市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130701000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130702000000,
                "name": "桥东区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130703000000,
                "name": "桥西区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130705000000,
                "name": "宣化区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130706000000,
                "name": "下花园区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130708000000,
                "name": "万全区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130709000000,
                "name": "崇礼区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130722000000,
                "name": "张北县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130723000000,
                "name": "康保县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130724000000,
                "name": "沽源县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130725000000,
                "name": "尚义县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130726000000,
                "name": "蔚县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130727000000,
                "name": "阳原县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130728000000,
                "name": "怀安县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130730000000,
                "name": "怀来县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130731000000,
                "name": "涿鹿县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130732000000,
                "name": "赤城县",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130771000000,
                "name": "张家口市高新技术产业开发区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130772000000,
                "name": "张家口市察北管理区",
                "level": 3,
                "pcode": 130700000000
            }, {
                "code": 130773000000,
                "name": "张家口市塞北管理区",
                "level": 3,
                "pcode": 130700000000
            }]
        }, {
            "code": 130800000000,
            "name": "承德市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130801000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130802000000,
                "name": "双桥区",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130803000000,
                "name": "双滦区",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130804000000,
                "name": "鹰手营子矿区",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130821000000,
                "name": "承德县",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130822000000,
                "name": "兴隆县",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130824000000,
                "name": "滦平县",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130825000000,
                "name": "隆化县",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130826000000,
                "name": "丰宁满族自治县",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130827000000,
                "name": "宽城满族自治县",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130828000000,
                "name": "围场满族蒙古族自治县",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130871000000,
                "name": "承德高新技术产业开发区",
                "level": 3,
                "pcode": 130800000000
            }, {
                "code": 130881000000,
                "name": "平泉市",
                "level": 3,
                "pcode": 130800000000
            }]
        }, {
            "code": 130900000000,
            "name": "沧州市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 130901000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130902000000,
                "name": "新华区",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130903000000,
                "name": "运河区",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130921000000,
                "name": "沧县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130922000000,
                "name": "青县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130923000000,
                "name": "东光县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130924000000,
                "name": "海兴县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130925000000,
                "name": "盐山县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130926000000,
                "name": "肃宁县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130927000000,
                "name": "南皮县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130928000000,
                "name": "吴桥县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130929000000,
                "name": "献县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130930000000,
                "name": "孟村回族自治县",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130971000000,
                "name": "河北沧州经济开发区",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130972000000,
                "name": "沧州高新技术产业开发区",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130973000000,
                "name": "沧州渤海新区",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130981000000,
                "name": "泊头市",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130982000000,
                "name": "任丘市",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130983000000,
                "name": "黄骅市",
                "level": 3,
                "pcode": 130900000000
            }, {
                "code": 130984000000,
                "name": "河间市",
                "level": 3,
                "pcode": 130900000000
            }]
        }, {
            "code": 131000000000,
            "name": "廊坊市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 131001000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131002000000,
                "name": "安次区",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131003000000,
                "name": "广阳区",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131022000000,
                "name": "固安县",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131023000000,
                "name": "永清县",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131024000000,
                "name": "香河县",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131025000000,
                "name": "大城县",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131026000000,
                "name": "文安县",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131028000000,
                "name": "大厂回族自治县",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131071000000,
                "name": "廊坊经济技术开发区",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131081000000,
                "name": "霸州市",
                "level": 3,
                "pcode": 131000000000
            }, {
                "code": 131082000000,
                "name": "三河市",
                "level": 3,
                "pcode": 131000000000
            }]
        }, {
            "code": 131100000000,
            "name": "衡水市",
            "level": 2,
            "pcode": 130000000000,
            "children": [{
                "code": 131101000000,
                "name": "市辖区",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131102000000,
                "name": "桃城区",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131103000000,
                "name": "冀州区",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131121000000,
                "name": "枣强县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131122000000,
                "name": "武邑县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131123000000,
                "name": "武强县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131124000000,
                "name": "饶阳县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131125000000,
                "name": "安平县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131126000000,
                "name": "故城县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131127000000,
                "name": "景县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131128000000,
                "name": "阜城县",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131171000000,
                "name": "河北衡水高新技术产业开发区",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131172000000,
                "name": "衡水滨湖新区",
                "level": 3,
                "pcode": 131100000000
            }, {
                "code": 131182000000,
                "name": "深州市",
                "level": 3,
                "pcode": 131100000000
            }]
        }]
    }]
    View Code

    shell.py根据model关系分析如何导入数据库


    # 遍历数据库拿到每个单独省份的数据
    for
    i in area:
      # 取到省份的名字 sup_name
    = i["name"]
      # 创建省份对象 a
    = Area.objects.create(name=sup_name, parent=None) # print(sup_name)
      
      # 遍历省份下面的市 for c in i["children"]:
         # 取到市的名字 sub_name
    = c["name"] print(sup_name, '----->' , sub_name)
         上海市 市辖区 普陀区
         # 创建市对象,并且把上一级的关联的省作为父级ID b
    = Area.objects.create(name=sup_name,parent=a)
         # 取到市下面关联的县或者区的数据 d
    = c.get("children") if d:
            # 遍历出来每个市下面的县或者区
    for j in d:
              # 取到县或者区的名字 sud_name
    = j["name"] print(sup_name, sub_name, sud_name)
              # 创建县对象,并且把上一级的关联的市的ID作为县的父ID f
    = Area.objects.create(name=sud_name, parent=b)

    models.py

    class Area(models.Model):
    
        name = models.CharField(max_length=20, verbose_name='名称')
        parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='上级行政区划')
    
        class Meta:
            db_table = 'ChinaArea'
            verbose_name = '行政区划'
            verbose_name_plural = verbose_name

    serializers.py

    from rest_framework import serializers
    
    from .models import Area
    
    
    class AreaSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Area
            fields = ('id', 'name')
    
    
    class SubAreaSerializer(serializers.ModelSerializer):
    
        subs = AreaSerializer(many=True, read_only=True)
        fields = ('id', 'name', 'subs')

     views.py 查询三级联动

    class AreaViewSet(ModelViewSet):
    
        def get_queryset(self):
            if self.action == 'list':
                return Area.objects.filter(parent=None)
            return Area.objects.all()
    
        def get_serializer_class(self):
            if self.action == 'list':
                return AreaSerializer
            return SubAreaSerializer
  • 相关阅读:
    关于论文绘图的一些拾遗
    在线word论文生成的方法
    用LyX写中文幻灯片
    关于KO信息
    关于中文期刊LaTeX的CCT相关
    Android NDK and OpenCV Development With Android Studio
    android上的JAVA8:使用retrolambda
    WebSocket学习笔记——无痛入门
    html5利用websocket完成的推送功能
    [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888
  • 原文地址:https://www.cnblogs.com/tangda/p/12766508.html
Copyright © 2020-2023  润新知