Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新Django数据库-脚本_Python_Database_Django_Api_Persistence - Fatal编程技术网

Python 更新Django数据库-脚本

Python 更新Django数据库-脚本,python,database,django,api,persistence,Python,Database,Django,Api,Persistence,因此,我是Django的新手,我正在开发一个应用程序,它使用一个脚本,该脚本使用一个外部API以Python字典的形式获取数据,然后将这些数据加载到我的Django数据库中,最后,数据库显示在一个HTML表中。这里是我感到困惑的地方:我在views.py中运行这个脚本,例如: def index(request): data = get_data() #print data for x in data: (newModel1, s_created)

因此,我是Django的新手,我正在开发一个应用程序,它使用一个脚本,该脚本使用一个外部API以Python字典的形式获取数据,然后将这些数据加载到我的Django数据库中,最后,数据库显示在一个HTML表中。这里是我感到困惑的地方:我在views.py中运行这个脚本,例如:

def index(request):
    data = get_data()
    #print data
    for x in data:
            (newModel1, s_created) = Model1.objects.get_or_create(model1_name = x)
            if s_created:
                    newModel1.save()
            for y in data[x]:
                    (newModel2, b_created) = Model2.objects.get_or_create(model2_name=y)
                    if b_created:
                            newModel2.save()

    return render_to_response('jenkins_slog/index.html', {'servers': Server$
'''

很明显,我不想每次加载页面时都运行这个脚本,因为那样效率会非常低。像这样的脚本在哪里更新,比如说,每五分钟更新一次

备选方案很多,让我们列出我的最爱: