0

In order to monitor mysql table change in django, I have written some codes as follows,

while not find_close_signal():
    time.sleep(10)
    if MyProject.models.MyModel.objects.all().exists():
        some_execution()

However, it doesn't work. If there is no entries in the table at the beginning, then some_execution() will never run even later there are records populated into that table through other out-band ways.

Does anyone ever meet such kind of issue?

I also found in "manage.py shell", this problem happens exactly the same: any other entries added into db out of this shell can not be found in this shell. Is this true or I've made some mistake? Thanks

William
  • 239
  • 1
  • 2
  • 11

3 Answers3

1

not sure that this is an issue, but it may be that you have this code executed inside a transaction, so in such a case you can't see any changes.

one more thing is that you can skip ".all()" in this check.

Jerzyk
  • 3,662
  • 23
  • 40
0

Take a look at my answers here:

Why doesn't this loop display an updated object count every five seconds?

How do I deal with this race condition in django?

Community
  • 1
  • 1
Tomasz Zieliński
  • 16,136
  • 7
  • 59
  • 83
0

You can mark the db as 'dirty' so to make Django discarding cached results:

from django.db import transaction
transaction.set_dirty()
Don
  • 16,928
  • 12
  • 63
  • 101