【问题标题】:Updating Django Database - Script更新 Django 数据库 - 脚本
【发布时间】:2014-08-29 10:52:28
【问题描述】:

所以我是 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$
'''

我显然不想在每次加载页面时都运行这个脚本,因为那样会非常低效。像这样的脚本去哪里以便它们更新,比如说,每五分钟?

【问题讨论】:

    标签: python database django api persistence


    【解决方案1】:

    替代品很多,让我们列出我的最爱:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 2011-01-29
      相关资源
      最近更新 更多