最简单的方式来编写一个访问Django数据库功能的Python程序 [英] Easiest way to write a Python program with access to Django database functionality

查看:123
本文介绍了最简单的方式来编写一个访问Django数据库功能的Python程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站可以定期从RSS Feed中获取信息(目前手动,这是我的问题)。这是一个正常的Django视图,这在我看来不是很好。我想要一个使用cronjob运行的Python程序,而不是手动访问正确的URL来更新信息。



什么是最简单的方法Python程序可以访问我的特定Django应用程序和Django ORM?

解决方案

  from django .core.management import setup_environ 
from django.core.mail import EmailMultiAlternatives,send_mail
from django.contrib.auth.models import User

import settings
from my .app.models import *

setup_environ(settings)

这是怎么回事我做了一个电子邮件通知各方每日更新。 .py居住在我的django应用程序的根目录,所以导入设置将反映出来。


I have a website which fetches information from RSS feeds periodically (well, currently manually, and this is my problem). This is currently implemented as a normal Django view, which isn't very nice in my opinion. I'd like to have a Python program which is run using a cronjob instead of manually visiting the correct URL to update the information.

What is the easiest way to make a Python program have access to my particular Django application and the Django ORM?

解决方案

from django.core.management import setup_environ
from django.core.mail import EmailMultiAlternatives, send_mail
from django.contrib.auth.models import User

import settings
from my.app.models import *

setup_environ(settings)

This was how I did it for a cron that emailed parties daily updates. The .py lived in the root of my django app, so the import settings would reflect that accordingly.

这篇关于最简单的方式来编写一个访问Django数据库功能的Python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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