class Command(BaseCommand):
def handle(self, *args, **kwargs):
try:
while True:
self._fetch_data()
time.sleep(10)
except KeyboardInterrupt:
pass
def _fetch_data(self):
data = ... # fetch data here
MyModel.objects.create(foo=data.foo, bar=data.bar) # insert into db
Djangoprovides facilities用于编写命令行应用程序。通过这种方式,您可以访问web进程中的所有内容,包括模型。你知道吗
并发是由数据库处理的,而不是django,所以您不必担心这个问题。如果您必须同时写入多个数据并且它们不能不同步,那么您可能只想使用transactions。你知道吗
假设上述类位于某个应用程序的
management/commands/mycommand.py
文件夹中,请使用manage.py
执行它:相关问题 更多 >
编程相关推荐