【发布时间】:2019-06-04 04:34:32
【问题描述】:
我有一个模型叫Poll,这个模型和另一个叫Choice 的模型有一对多的关系。 1 轮询可以有很多选择。每次保存投票时,我都想在 Elasticsearch 中保存该投票。
型号:
class Poll(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
question = models.CharField(max_length=200)
def indexing(self):
obj = QuestionIndex(
meta={'id': self.id},
question=self.question,
choices=self.choice_set.count(), # How many choices are there?
)
obj.save()
return obj.to_dict(include_meta=True)
索引
class QuestionIndex(Document):
question = Text()
choices = Integer()
class Index:
name = 'questions'
我已经在 Django 中添加了一个信号来执行以将数据保存在 Elasticsearch 中。这很好用,但是,即使我添加了选择,我也看到选择的值始终为 0。选择确实保存在我的数据库中,但选择的计数始终为 0。我做错了什么?
【问题讨论】:
标签: django elasticsearch django-models