最好的方法来更新我的Django模型来自外部api源吗? [英] Best way to update my django model coming from an external api source?

查看:42
本文介绍了最好的方法来更新我的Django模型来自外部api源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过请求api源获取数据,然后将其放入django模型中.但是,数据每天都会更新..那么如何在不每次渲染的情况下更新这些数据?

I am getting my data through requesting an api source, then I put it in my django model. However, data update daily.. so how can I update these data without rendering it everytime?

def index (request):
    session = requests.Session()
    df = session.get('https://api.coincap.io/v2/assets')
    response= df.json()
    coin = response['data']
    final_result = coin.to_dict('records')
    for coin in final_result:
        obj, created = Coincap.objects.update_or_create(
            symbol = coin['symbol'],
            name = coin['name'],
            defaults = {
                'price': coin['priceUsd']
                })
    return render(request, '/home.html/')

现在,如果要更新数据,我必须转到/home.html .但是,我的目标是稍后对其进行序列化并使其成为REST api数据,因此我不再涉及django模板.无论如何,在我执行 manage.py runserver 后,每天对其内部进行一次更新?

Right now, I have to go to /home.html , if I want my data update. However, my goal is to later serialize it and make it REST api data, so I wouldn't touch django template anymore. Anyway for it to update internally once a day after i do manage.py runserver?

推荐答案

一种简单且常见的解决方案是创建

One simple and common solution is to create a custom Django admin command and use Cron to run it at specified intervals. You can write a command's code to your liking and it can have access to all of the models, settings and other parts of your Django project.

您将使用Django模型在新的 Command 类的 handle()方法(显然是 request 参数不再需要).然后,例如,如果您已将命令命名为 update_some_data ,则可以将其作为 python manage.py update_some_data 来运行.

You would put your code making a request and writing data to the DB, using your Django models, in your new Command class's handle() method (obviously request parameter is no longer needed). And then, if for example you have named your command update_some_data, you can run it as python manage.py update_some_data.

假定Cron存在并且正在计算机上运行.然后,您可以设置Cron以指定的时间间隔为您运行此命令,例如,创建文件/etc/cron.d/your_app_name 并放入

Assuming Cron exists and is running on the machine. Then you could setup Cron to run this command for you at specified intervals, for example create a file /etc/cron.d/your_app_name and put

0 4 * * * www-data /usr/local/bin/python /path/to/your/manage.py update_some_data >> /var/log/update_some_data.log  2>&1

这将使您的更新每天在04:00完成.如果您的命令将提供任何输出,它将被写入/var/log/update_some_data.log 文件.当然,这只是一个示例,因此您的服务器用户正在运行您的应用程序(此处为 www-data ),并指向服务器上Python可执行文件的路径(/usr/local/bin/python 此处),以适合特定用途.

This would make your update be done everyday at 04:00. If your command would provide any output, it will be written to /var/log/update_some_data.log file. Of course this is just an example, so your server user running your app (www-data here) and path to the Python executable on the server (/usr/local/bin/python here) should be adjusted for particular use.

请参阅链接以获取更多指导.

See links for further guidance.

这篇关于最好的方法来更新我的Django模型来自外部api源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆