Never can pass Django model objects to Celery tasks?

I am grateful for Celery integration with Django. It is great for things

like Celery tasks that send emails.

I understand that Celery tasks cannot accept Django model objects as arguments. Is that a

big limitation? There are lots of things that would be nice to push to Celery if possible that

involve Django model objects like updates and deletions no?

Thanks,

chris

Typically you pass just the primary key(s) of the model. The first line then does a Model.objects.get(pk=model_pk)

If it doesn’t exist then you can trigger a retry of the task.

The benefit of this approach is you get the latest version of the model over a potentially stale version passed through memory. Also data needs to be serialized and deserialized to be stored on the queue, so sticking to simpler data types will make future maintenance/upgrades easier.

1 Like