简体   繁体   English

如何在formset中呈现django haystack搜索结果

[英]how to render django haystack search results in formset

I am using haystack for search. 我正在使用干草堆进行搜索。 I need to allow the user to add additional data to search entries after a search and so I believe I need to use the search results in a formset or list of forms. 我需要允许用户在搜索后向搜索条目添加其他数据,因此我相信我需要在表单集或表单列表中使用搜索结果。

For example when search for food I would be able to add the serving size: 例如,当搜索食物时,我可以添加份量:

在此处输入图片说明

How can I accomplish this? 我该怎么做?

Your question does not contain enough information for a detailed answer but based on the information you've given I would suggest you take a look at creating unbound forms using the Django Formfactory and populating them with data. 您的问题没有足够的信息来提供详细的答案,但是根据您所提供的信息,我建议您看一下使用Django Formfactory创建未绑定的表单并将其填充数据的方法。

You can then use these populated forms in the way you requested. 然后,您可以按照请求的方式使用这些填充的表单。

I'll give you some example code to get you started: 我将为您提供一些示例代码,以帮助您入门:

    def _create_form_instances(self, model_name=None, exclude=None):
        """Creates an form instance for the provided model_name"""

        for content_type in self.content_types:
            if model_name == content_type._meta.object_name:
                form = modelform_factory(content_type, exclude=exclude)

                return form

     #The following code comes from a different function:

     unbound_form = self._create_form_instances(
                    model_name=item._meta.object_name,
                    exclude=('page', 'content_html'))

                instance_to_form_mapping = {'content': item.content}

                bound_form = unbound_form(instance_to_form_mapping)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM