我从 django-celery 收到了一个错误,这让我在一天中的大部分时间里完全被难住了。
IM 使用 django-haystack 并有一个 celery 任务,只要作为搜索索引一部分的模型更新,就会运行该任务。我有一个连接到模型 post_save 和 post_delete 信号的自定义 QueuedSearchIndex 类。这适用于一个模型,但不适用于另一个模型,即来自 django.contrib.auth 的用户模型
创建/更新用户模型时,celery 会出现以下错误。
Problem installing fixture '/believein/data/www/platform/believein/fixtures/user.json': Traceback (most recent call last):
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 196, in handle obj.save(using=using)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save models.Model.save_base(self.object, using=using, raw=True)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/django/db/models/base.py", line 565, in save_base
created=(not record_exists), raw=raw, using=using)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/believein/data/www/platform/main/utils/search.py", line 156, in update_object
update_search_index.delay(self, instance, self.backend)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/celery/app/task.py", line 343, in delay
return self.apply_async(args, kwargs)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/celery/app/task.py", line 469, in apply_async
**options)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/celery/app/amqp.py", line 214, in publish_task
**kwargs)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/kombu/messaging.py", line 150, in publish
compression, headers)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/kombu/messaging.py", line 201, in _prepare
body) = encode(body, serializer=serializer)
File "/believein/data/python-virtualenvs/believein-platform/local/lib/python2.7/site-packages/kombu/serialization.py", line 153, in encode
payload = encoder(data)
File "/believein/data/python-virtualenvs/believein-platform/lib/python2.7/copy_reg.py", line 71, in _reduce_ex
state = base(self)
TypeError: Initialization arguments are not supported
正如你所看到的,这个例子是在加载夹具时提出的,但在正常编辑模型时是一样的。用户的新实例和已存在的更新实例都是这种情况。我的任务如下所示
@task
def update_search_index(index, instance, backend):
"""task to update the solr index when a model is updated
:param index: instance of the ``SearchIndex``
:param instance: the model that was updated
:type instance: ``django.db.models.Model``
:param backend: the search back instance
:type backend: ``haystack.backends.solr_backend``
see ``main.utils.search.QueuedSearchIndex`` for more info
"""
backend.update(index, [instance])
searchindex 类如下所示
class QueuedSearchIndex(RealTimeSearchIndex):
def update_object(self, instance, **kwargs):
"""Handles updating the index when a model is updated by pushing
the update through celery
:param instance: the instance of the model that was updated
:type instance: django.db.models.Model
"""
if self.should_update(instance, **kwargs):
update_search_index.delay(self, instance, self.backend)
值得一提的是,User 模型与 UserProfile 模型具有 One2One 关系,该模型在 User 模型上具有 post_save 信号,以在已经存在一个剂量的情况下创建 UserProfile 实例。