Django 1.5 manage.py shell doesn't keep up with database - need to disable caching?

175 views Asked by At

I've just managed to diagnose this strange problem in a manage.py script I've created.

The script is supposed to update a local cache with new data at regular intervals.

However, the new data can't be seen from the script, even though it's really there.

The problem seems to extend to manage.py shell as well.

If I run a shell AFTER the new objects are added, and manually run my query (... objects.filter(timestamp__gt=recent) ...), then I get results.

If I run a shell BEFORE the new objects are added, then add some objects via the interwebs, and then run my query... no results.

How can I disable this weird behaviour?

1

There are 1 answers

0
Alex On

Sorry, found a related question which solves this:

How to disable Django query cache?

The problem is that (as Peter DeGlopper suggested) Django automatically sets up a transaction, which stops you from seeing new data.

The solution is (from Kekoa's answer)

from django.db import transaction
transaction.enter_transaction_management()
transaction.commit() # Whenever you want to see new data