[Answer]-Django with tastypie how to refresh queryset?

1👍

Django will cache the results within a queryset, but not the results within unrelated querysets.

The following code will result in two database queries:

list(SomeTable.objects.all())  # First db query.
list(SomeTable.objects.all())  # Second db query unrelated to first.

The symptoms and your connection.close() solution are consistent with a MySQL transaction issue. A new connection on each query will result in a different transaction.

It seems as though a transaction is being started in your Django code, but never committed or rolled back. When the transaction starts, further reads/writes in that transaction will only see the state of the database at the start of the transaction, plus and updates made in that transaction. If another process starts a session (eg your data mining script) and inserts data, it will not be seen by the Django transaction until the existing transaction is closed.

Without seeing your code it’s impossible to tell why there would be a transaction started but not completed. To verify this assumption, turn on the MySQL query log and examine its contents. You should see a start transaction query without a corresponding commit or rollback.

0👍

Add “transaction-isolation = READ-COMMITTED” to my.cnf. More details here: Django: how to refresh or reload models from database

👤Terry

Leave a comment