django批量插入数据,存入数据
作者:
代表群众 | 来源:发表于
2020-02-06 10:27 被阅读0次import os
import sys
# databases就是项目名称,本文件就是要放在项目文件下
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "databases.settings")
'''python
Django 版本大于等于1.7的时候,需要加上下面两句
import django
django.setup()
否则会抛出错误 django.core.exceptions.AppRegistryNotReady:
Models aren't loaded yet.
'''
import django
if django.VERSION >= (1, 7): # 自动判断版本
django.setup()
def main():
from pick_gene.models import Gene
for line in open(sys.argv[1], 'r'):
if line.startswith('>'):
index = 1
id = line[1:]
else:
index = 0
seq = line
if index==0:
# gene_id gene_seq就是项目里的字段名
Gene.objects.create(gene_id=id, gene_seq=seq)
if __name__ == "__main__":
main()
print('Done!')
本文标题:django批量插入数据,存入数据
本文链接:https://www.haomeiwen.com/subject/uaeyxhtx.html
网友评论