我从django-celery那里得到一个错误,让我在一天中的大部分时间里完全被困住了。
IM使用django-haystack并且只要更新了作为搜索索引一部分的模型,就会运行芹菜任务。我有一个自定义QueuedSearchIndex类连接到模型post_save和post_delete信号。这适用于一个模型,但不适用于另一个模型,即django.contrib.auth
中的用户模型创建/更新用户模型时,芹菜会出现以下错误。
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实例。