我有2个在Django中看起来像这样的模型:
class Poll(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
question = models.CharField(max_length=200)
class Choice(models.Model):
question = models.ForeignKey(Poll, on_delete=models.CASCADE)
choice = models.CharField(max_length=120)
vote_count = models.IntegerField(default=0)
每次用户添加新民意调查时,都应将其保存到Elasticsearch中。保存方式应如下所示:
{
"question": "Dogs or cats?",
"choices": [
{
"choice": "Dogs"
},
{
"choice": "Cats"
}
]
}
在Poll模型中,我创建了一个名为indexing的函数,如下所示:
def indexing(self):
obj = PollIndex(
meta={'id': self.id},
question=self.question,
choices=self.choice_set,
)
obj.save()
return obj.to_dict(include_meta=True)
我这样定义索引:
class PollIndex(Document):
question = Text()
choices = Nested()
但是,我收到一条错误消息:AttributeError: 'RelatedManager' object has no attribute 'items'
我在做什么错?