Changes in DB data not reflecting in the Django queryset in a continuously looping script

你离开我真会死。 提交于 2019-12-22 12:56:11

问题


I am using Django's ORM to get newly added entries from a Db and pass them to a Messaging queue. I am doing this in an infinite while loop , The PROBLEM in every loop iteration I am getting the same queryset even when I have added/deleted/edited entries when this script is running,

The code goes like this :

while True :
    sleep(10 seconds)
    # Below is the problem line, I get the same query-set every time in new_objects
    # even when I have added/deleted/edited entries while this daemon is running.
    new_objects = Model.objects.filter(some condition)

    # Process new_objects and send them to MQ
    .
    . and so on

What should I do to reflect the latest data in each iteration ?


回答1:


This isn't anything to do with caching. This is to do with transactions.

A continuously-running script by default runs within a single transaction, and so will never see changes outside that transaction. You'll need to start a new transaction manually on every iteration - see the transaction documentation on how to do that.



来源:https://stackoverflow.com/questions/6887901/changes-in-db-data-not-reflecting-in-the-django-queryset-in-a-continuously-loopi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!