【问题标题】:How do you convert a Django model with ManytoMany field to an elasticsearch_dsl DocType class?如何将具有 ManytoMany 字段的 Django 模型转换为 elasticsearch_dsl DocType 类?
【发布时间】:2018-01-02 21:39:36
【问题描述】:

我正在使用 elasticsearch_dsl 来帮助与 elasticsearch 交互,这是我目前拥有的模型,我正在尝试将其重新创建为 DocType:

class HouseIndex(DocType):
    house_type = String()
    #people
    sold = Boolean()
    built_datetime = Date()
    #alerts
    # associated_locations
    hash = String()


class House(models.Model):
    house_type = models.ForeignKey(HouseType, db_index=True,
                                   on_delete=models.CASCADE)
    people = models.ManyToManyField(to='Person', db_index=True,
                                      through='PersonToHouseMap')
    sold = models.BooleanField(default=False)
    built_datetime = models.DateTimeField()
    alerts = models.ManyToManyField(Alert)
    associated_locations = models.ManyToManyField(to='Location')
    hash = models.CharField(max_length=64, unique=True, null=True)
    objects = HouseManager()

但我不确定当它是多对多字段时该怎么做。例如人员、警报和相关位置。

【问题讨论】:

    标签: django elasticsearch elasticsearch-dsl


    【解决方案1】:

    这实际上取决于您要查询的内容,但我可能会使用包含Location 对象列表的Nested 字段。您可以在我的示例 repo 中看到类似的内容 - https://github.com/HonzaKral/es-django-example/blob/master/qa/models.py#L48

    【讨论】:

    • 方法to_search是有效的序列化器吗?
    • 是的,它是 - 它采用模型实例并生成 DocType 的实例。随着对 6.0 中的 Nested 的更改,即使是 Comment.to_search 也会生成 InnerDoc 子类的实例,而不仅仅是字典。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多