I'm saving an object in Django Admin (Django 1.8) and passing them to Celery Task. Unfortunately, I got an error [sometimes!]: "Matching query doesn't exist". I know that this is a problem with transaction, but what is the best way to solve that problem?
class MyModelAdmin(admin.ModelAdmin)
def save_model(self, request, obj, form, change):
super(MyAdmin, self).save_model(request, obj, form, change)
if not change:
celery_task.delay(obj.pk)
@app.task()
def celery_task(obj_pk):
MyModel.objects.get(pk=obj_pk)
The problem is that a whole view in Django Admin is in transaction.atomic() block. And sometimes celery runs faster that the end of transaction. I'm wondering what is the best way to get this around. Adding an eta while calling celery_task is some creepy idea I think (or maybe not?) - celery_task.apply_async((obj.pk,), eta=+10 seconds)
If someone still looking for this one: Django docs
from django.db import transaction
class MyModelAdmin(admin.ModelAdmin)
def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
if not change:
transaction.on_commit(lambda: celery_task.delay(obj.pk))
...
@app.task()
def celery_task(obj_pk):
MyModel.objects.get(pk=obj_pk)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With