Django - 根据前端用户提供的信息进行查询。并非每次都会使用所有过滤器。如何做到这一点?
Django - Make query based on information provided from a user on front end. Not all filters will be used every time. How to achieve this?
我有问题。我正在构建一个应用程序,用户可以在表单中 select 一些过滤选项。并非每次都会使用所有选项。我不知道如何构建正确的 Django 查询。
起初我尝试过这种方法:
if mileage:
if mileage_less_more == 'mileage_less_than':
cars = cars.objects.filter(price__lte=mileage)
if mileage_less_more == 'mileage_more_than':
cars = cars.objects.filter(price__gte=mileage)
if production_year:
if production_year_less_more == 'production_year_less_than':
cars = cars.objects.filter(production_year__lte=production_year)
if production_year_less_more == 'production_year_more_than':
cars = cars.objects.filter(production_year__gte=production_year)
if production_year_less_more == 'production_year_exact':
cars = cars.objects.filter(production_year=production_year)
我假设它会像 python 中的任何其他变量一样工作,即使不使用上述过滤器之一(例如里程将为 None),它也会赢'执行。
但是据我了解,Django 不支持这种方法。
然后我用 f 字符串尝试了很多奇怪的东西,但它也没有用。
然后我尝试了这个方法:
if mileage:
if mileage_less_more == 'mileage_less_than':
mileage_qs = Car.objects.filter(price__lte=mileage)
if mileage_less_more == 'mileage_more_than':
mileage_qs = Car.objects.filter(price__gte=mileage)
else:
mileage_qs = Car.objects.all()
if production_year:
if production_year_less_more == 'production_year_less_than':
production_year_qs = Car.objects.filter(production_year__lte=production_year)
if production_year_less_more == 'production_year_more_than':
production_year_qs = Car.objects.filter(production_year__gte=production_year)
if production_year_less_more == 'production_year_exact':
production_year_qs = Car.objects.filter(production_year=production_year)
else:
production_year_qs = Car.objects.all()
cars_final = Car.objects.all().intersection( mileage_qs, production_year_qs)
并且有效。但是以后会出问题。我需要对此 cars_final
项进行更多过滤。而且Django也不支持交集后过滤
我可以稍后在我的代码中尝试粘贴整个 cars_final
,然后在 intersection()
应用额外的 filter()
之前,但它很快就会变得非常混乱。
我确信有更优雅的方法来做到这一点,但我不知道如何做,我也无法 google。有人可以帮我吗?
你应该看看 https://django-filter.readthedocs.io/en/stable/。它拥有您在过滤方面所需的一切,并且易于设置。尝试创建自己的过滤器比人们想象的要复杂。
如果您真的想创建自己的过滤器,可以在将查询字典传递给查询集之前使用关键字参数来构建查询字典。像这样:
data = {}
if variable == 1:
data['key'] = 'foo'
elif variable == 2:
data['key_2'] = 'bar'
if data:
MyModel.objects.filter(**data)
else:
MyModel.objects.all()
我有问题。我正在构建一个应用程序,用户可以在表单中 select 一些过滤选项。并非每次都会使用所有选项。我不知道如何构建正确的 Django 查询。
起初我尝试过这种方法:
if mileage:
if mileage_less_more == 'mileage_less_than':
cars = cars.objects.filter(price__lte=mileage)
if mileage_less_more == 'mileage_more_than':
cars = cars.objects.filter(price__gte=mileage)
if production_year:
if production_year_less_more == 'production_year_less_than':
cars = cars.objects.filter(production_year__lte=production_year)
if production_year_less_more == 'production_year_more_than':
cars = cars.objects.filter(production_year__gte=production_year)
if production_year_less_more == 'production_year_exact':
cars = cars.objects.filter(production_year=production_year)
我假设它会像 python 中的任何其他变量一样工作,即使不使用上述过滤器之一(例如里程将为 None),它也会赢'执行。 但是据我了解,Django 不支持这种方法。
然后我用 f 字符串尝试了很多奇怪的东西,但它也没有用。
然后我尝试了这个方法:
if mileage:
if mileage_less_more == 'mileage_less_than':
mileage_qs = Car.objects.filter(price__lte=mileage)
if mileage_less_more == 'mileage_more_than':
mileage_qs = Car.objects.filter(price__gte=mileage)
else:
mileage_qs = Car.objects.all()
if production_year:
if production_year_less_more == 'production_year_less_than':
production_year_qs = Car.objects.filter(production_year__lte=production_year)
if production_year_less_more == 'production_year_more_than':
production_year_qs = Car.objects.filter(production_year__gte=production_year)
if production_year_less_more == 'production_year_exact':
production_year_qs = Car.objects.filter(production_year=production_year)
else:
production_year_qs = Car.objects.all()
cars_final = Car.objects.all().intersection( mileage_qs, production_year_qs)
并且有效。但是以后会出问题。我需要对此 cars_final
项进行更多过滤。而且Django也不支持交集后过滤
我可以稍后在我的代码中尝试粘贴整个 cars_final
,然后在 intersection()
应用额外的 filter()
之前,但它很快就会变得非常混乱。
我确信有更优雅的方法来做到这一点,但我不知道如何做,我也无法 google。有人可以帮我吗?
你应该看看 https://django-filter.readthedocs.io/en/stable/。它拥有您在过滤方面所需的一切,并且易于设置。尝试创建自己的过滤器比人们想象的要复杂。
如果您真的想创建自己的过滤器,可以在将查询字典传递给查询集之前使用关键字参数来构建查询字典。像这样:
data = {}
if variable == 1:
data['key'] = 'foo'
elif variable == 2:
data['key_2'] = 'bar'
if data:
MyModel.objects.filter(**data)
else:
MyModel.objects.all()