gpt4 book ai didi

django 模型搜索表​​单

转载 作者:行者123 更新时间:2023-12-04 23:51:04 25 4
gpt4 key购买 nike

首先,我在发帖前做了功课并环顾四周!我的问题似乎是一个非常基本的问题,以前肯定已经讨论过了。

我现在在看 Django-filter作为一个潜在的解决方案,但想就这是否是正确的方法以及是否有任何其他解决方案提供一些建议。

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

我想为用户将用于搜索数据库的每个模型(在单独的 View 中)创建一个单独的表单。搜索表单将仅包含下拉框(select 小部件),其选项与用于填充数据库的表单相同,并添加了“任何”选项。

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

我简要地查看了 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 模型以便于阅读

属性/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

在您的实际 View 中(我仅将搜索表单作为我的详细信息 View 中的侧边栏元素应用:
# 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')

最后你的搜索结果 View (这是作为 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

关于django 模型搜索表​​单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com