django模型搜寻表格

用户名

首先,我完成了作业并在发布之前环顾四周!我的问题似乎是必须解决的非常基本的问题。

我现在正在将Django过滤器视为一种潜在的解决方案,但希望就此是否正确以及是否有其他解决方案提供一些建议。

我有10个模型的Django应用程序,每个模型都有几个字段。大多数字段是ChoiceField用户使用带有默认select小部件的表单填充的每个模型都有一个单独的表格。

我想为用户将用于搜索数据库的每个模型(在单独的视图中)创建一个单独的表单。搜索表单将仅包含select与用于填充数据库的表单具有相同选择的下拉框(窗口小部件),并增加了“ any”选项。

我知道如何使用.object.filter(),但是“ any”选项对应于在过滤器中不包含特定字段,并且我不确定如何根据用户的选择将模型字段添加到过滤器中

我简要地看了一下Haystack作为一种选择,但它似乎是为全文搜索而不是我追求的“模型文件搜索”而设计的。

样本模型(简体):

class Property():             
      TYPE_CHOICES = (‘apartment’, ‘house’, ‘flat’)        
      type = charfield(choices=TYPE_CHOICES)
      LOC_CHOICES = (‘Brussels’, ‘London’, ‘Dublin’, ‘Paris’)
      location = charfield(choices=LOC_CHOICES)
      price = PostivieInteger()

用户只能选择“类型”,“位置”或两者(不等于ANY),在这种情况下,我会得到3种不同的过滤器:

Property.objects.filter(type=’apartment’)
Property.objects.filter(location=’Dublin’)
Property.objects.filter(type=’apartment’, location=’Dublin’)

主要问题:django-filter的最佳选择?

Question 1: what’s the best option of accomplishing this overall? 
Question 2: how do I add model fields to the filter based on user’s form selection?
Question 3: how do I do the filter based on user selection? (I know how to use .filter(price_lt=).exclude(price_gt=) but again how do I do it dynamically based on selection as “ANY” would mean this is not included in the query)
欺骗

我有一个类似的案例,例如您的(房地产项目),我得到了以下方法,您可以根据自己的需要进行细化...我删除了select_related和prefetch_related模型,以便于阅读

properties / forms.py:

class SearchPropertyForm(forms.Form):

    property_type = forms.ModelChoiceField(label=_("Property Type"), queryset=HouseType.objects.all(),widget=forms.Select(attrs={'class':'form-control input-sm'}))
    location = forms.ModelChoiceField(label=_('Location'), queryset=HouseLocation.objects.all(), widget=forms.Select(attrs={'class':'form-control input-sm'}))

然后在properties / views.py中

# Create a Mixin to inject the search form in our context 

class SeachPropertyMixin(object):
    def get_context_data(self, **kwargs):
        context = super(SeachPropertyMixin, self).get_context_data(**kwargs)
        context['search_property_form'] = SearchPropertyForm()
        return context

在您的实际视图中(我仅在我的detailview中将搜索表单作为侧边栏元素应用:

# Use Class Based views, saves you a great deal of repeating code...
class PropertyView(SeachPropertyMixin,DetailView):
    template_name = 'properties/view.html'
    context_object_name = 'house'
    ...
    queryset = HouseModel.objects.select_related(...).prefetch_related(...).filter(flag_active=True, flag_status='a')

最终,您的搜索结果视图(这是作为GET请求执行的,因为我们没有更改数据库中的任何数据,所以我们坚持使用GET方法):

# Search results should return a ListView, here is how we implement it:
class PropertySearchResultView(ListView):
    template_name = "properties/propertysearchresults.html"
    context_object_name = 'houses'
    paginate_by = 6
    queryset = HouseModel.objects.select_related(...).prefetch_related(...).order_by('-sale_price').filter(flag_active=True, flag_status='a')

    def get_queryset(self):
        qs = super(PropertySearchResultView,self).get_queryset()
        property_type = self.request.GET.get('property_type')
        location = self.request.GET.get('location')
        '''
        Start Chaining the filters based on the input, this way if the user has not 
        selected a filter it wont be used.
        '''
        if property_type != '' and property_type is not None:
            qs = qs.filter(housetype=property_type)
        if location != '' and location is not None:
            qs = qs.filter(location=location)
        return qs

    def get_context_data(self, **kwargs):
        context = super(PropertySearchResultView, self).get_context_data()
        ''' 
        Add the current request to the context 
        '''
        context['current_request'] = self.request.META['QUERY_STRING']
        return context

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Django中搜寻表格

来自分类Dev

使Django模型表格安全

来自分类Dev

在Django中将模型导入表格

来自分类Dev

在Django中将模型导入表格

来自分类Dev

以表格形式加载动态模型属性-Django

来自分类Dev

Django创建订单表格模型设计

来自分类Dev

以表格形式加载动态模型属性-Django

来自分类Dev

Django多值搜寻栏

来自分类Dev

搜寻论坛:无法搜寻其中包含表格的帖子

来自分类Dev

搜寻论坛:无法搜寻其中包含表格的帖子

来自分类Dev

搜寻表格后更改div样式

来自分类Dev

与搜寻索引相关的模型字段

来自分类Dev

如何解决Django模型表格无法保存

来自分类Dev

SSAS表格模型缓慢

来自分类Dev

SSAS表格-多个模型?

来自分类Dev

表格发送空模型

来自分类Dev

表格发送空模型

来自分类Dev

模型功能的Rails表格

来自分类Dev

进阶搜寻表格会显示更多表格以取得更多结果

来自分类Dev

django过滤查询集会导致“ AppRegistryNotReady:模型尚未加载”。与models.py中的表格

来自分类Dev

Django模型ForeignKey unique = True不以表格形式显示的最佳做法是什么?

来自分类Dev

将数据从Excel电子表格导入到Django模型

来自分类Dev

如何在Django的注册表格中添加2个模型?

来自分类Dev

我们可以格式化模型表格在Django模板上的显示方式吗

来自分类Dev

限制用户更新模型表格

来自分类Dev

3种关联模型的表格

来自分类Dev

表格模型度量未填充

来自分类Dev

最好为每个具有选项的单独列制作表格,或者在 django 中的模型类中制作选项元组

来自分类Dev

如何以表格形式呈现模型,并使用 django 使用复选框选择其中一些?

Related 相关文章

热门标签

归档