首先,我完成了作业并在发布之前环顾四周!我的问题似乎是必须解决的非常基本的问题。
我现在正在将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] 删除。
我来说两句