我遵循了许多示例和文档,但不能正确。
每当我更新模型时,我都在尝试制作我的Elasticsearch索引。使用"自动指标"未记录M2M的。
- 当我更改
Tag
内的值时,它应该更新所有Items
的索引,均为
我正在使用django-elasticsearch
(此处(,这是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)
好吧,它并不像我想象的那样"聪明"。您需要参考每个模型中的功能
class Tag(models.Model):
name = models.CharField(max_length=500, blank=True)
taglevel = models.IntegerField(null=True, blank=True)
@receiver(post_save, sender= Tag)
@receiver(post_delete, sender= Tag)
def index_tag(instance, **kwargs):
Item.objects.get().es.do_index()
def tag_index(instance, **kwargs):
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 = ItemEsSerializer
fields = ['title', 'tag']
@receiver(post_save, sender= Item)
def index_elastic(instance, **kwargs):
instance.es.do_index()