0
我遵循了許多示例和文檔,但無法正確理解。使用信號爲模型反向索引M2M
我試圖讓我的Elasticsearch索引,只要我更新模型。使用'autoindex'不會記錄M2M的。
- 當我改變內部
Tag
的值應該更新的所有Items
該指數是
我使用django-elasticsearch
(here),這是一個包裝周圍elasticsearch-py
。
當我嘗試connect(tag_index, sender=Tag.item.through)
它無法找到標籤,因爲它尚未被模型導入。
這是如何正確實現的?
class Tag(models.Model):
name = models.CharField(max_length=500, blank=True)
taglevel = models.IntegerField(null=True, blank=True)
def tag_index(instance, **kwargs):
Task.instance.es.do_index()
class Item(EsIndexable, models.Model):
title = models.CharField(max_length=100, blank=True)
tag = models.ManyToManyField('Tag', blank=True)
class Elasticsearch(EsIndexable.Elasticsearch):
serializer_class = TaskEsSerializer
fields = ['title', 'tag']
@receiver(post_save, sender= Task)
def index_elastic(instance, **kwargs):
instance.es.do_index()
m2m_changed.connect(tag_index, sender=Item.tag.through)