Django - Delay in creating database entry
django, postgresql, python
Solution
Try this
from django.db import transaction
with transaction.atomic():
instance = MyModel.objects.create(**kwargs)
tasks.my_task.delay(instance.id)
Problem
I have a Django app where I create a db entry in the view. I then want to perform background processing on the new entry. Instead of sending the created object to the task, I send the object's id then the background task can fetch the db object as explained here. Below is my code: ``` # In tasks.py @shared_task def my_task(model_id): my_model = MyModel.objects.get(pk=model_id) # Do stuff with my_model # In views.py: def some_view(request): if request.method == 'POST' and request.is_ajax(): instance = MyModel.objects.create(**kwargs) tasks.my_task.delay(instance.id) .... ``` However, when I try to get the object in the background task, I get matching query does not exist error. If I add `sleep(1)` before getting the object, it works as excepted. I do not understand why I'm getting this error, since the object should be in the DB? Does anybody know how to solve this? I don't really want to add a sleep command everywhere. I'm using Postgres as my DB.