Django ORM queries do not allow selection of new objects

Setup:

  • Python script A inserts data into the database every 15 minutes.
  • Python script B requests for the last 5 entries every few minutes

Both use django ORM, the same MySQL DB and the same DB user account (same settings.py file)

Problem:
B is able to retrieve only records inserted before it starts. As if B was working with a frozen DB, currently frozen B , first connected to the database.

How did it happen? Can I control this behavior in django?

+5
source share
3 answers

If you reuse the same Manager object, you need to keep in mind caching . To handle this, you need to manually update.

This will return the same results at each iteration:

 while True: same_every_time = AClass.objects.all().order_by('-id')[:5] sleep(300) 

For it to work correctly, you must add an update:

 while True: AClass.objects.update() updated_results = AClass.objects.all().order_by('-id')[:5] sleep(300) 
+9
source

Django itself uses a kind of container-driven persistence. This means that database transactions are committed by middleware during the feedback stage.

If you are using ORG Django ORM yourself, you must ensure that the transaction is actually committed:

 from django.db import transaction def my_task(whatever): MyModel.objects.create(...) # do whatever transaction.commit() return 'my_result' 
0
source

I found this stackoverflow question when trying to solve a similar situation using Django tests: in the test I modified the objects of the Django model, but the changes were not reflected in the database, so that the database reader in the second thread could detect the changes. The solution was to inherit my test class from TransactionTestCase instead of the standard django.test.TestCase.

0
source

Source: https://habr.com/ru/post/894826/


All Articles