Celery 不会遍历 Django 外键关系

Celery won't traverse Django foreign key relationships

我在 Heroku 上 运行 两个实例:
1. 独角兽
2.芹菜

在我的电子邮件模板中,我有一些效果:

orders.orderitem_set.all

当我通过 Web 实例(即没有 Celery)呈现这些电子邮件时,我得到了一个订单项目列表(如预期的那样)。

但是,当我用 Celery 渲染这些模板时,列表是空的。

为什么 Celery 不遍历模板中的外键关系,我如何知道范围内和范围外的内容?

最有可能的问题是 django 数据库对象在执行 celery 任务时过时了。

Deni Bertovic 在 https://denibertovic.com/posts/celery-best-practices/

中指出了这个问题

You shouldn't pass Database objects (for instance your User model) to a background task because the serialized object might contain stale data. What you want to do is feed the task the User id and have the task ask the database for a fresh User object.