gpt4 book ai didi

django - 如何在 Django 中创建多个过滤器?

转载 作者:行者123 更新时间:2023-12-05 05:31:30 25 4
gpt4 key购买 nike

我想在 Django 中创建多个过滤器。

下面的屏幕截图对此进行了解释。

enter image description here

用户可以选择不选择某些条件。

如果用户没有选择 Available from、Available till 和 Advance,那么我将在 views.py 中获得 None 的值。如果他没有选择类别或容量,那么我将得到一个空列表,否则我将得到一个类别或容量列表。

当没有或空列表时就会出现问题。

虽然我已经编写了代码并且运行良好,但我想知道是否有更好的方法来做到这一点?如果复杂性增加,我的代码可能会出现问题。

表单.py

class RoomForm(forms.Form):

ROOM_CATEGORIES = (
('Regular', 'Regular'),
('Executive', 'Executive'),
('Deluxe', 'Deluxe'),
('King', 'King'),
('Queen', 'Queen'),
)

category = forms.MultipleChoiceField(
required=False,
widget=forms.CheckboxSelectMultiple,
choices=ROOM_CATEGORIES,
)

ROOM_CAPACITY = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
)
capacity = forms.MultipleChoiceField(
required=False,
widget=forms.CheckboxSelectMultiple,
choices=ROOM_CAPACITY,
)

class TimeInput(forms.TimeInput):
input_type = 'time'
default=datetime.time()

available_from = forms.TimeField(
required=False,
widget=TimeInput(),
)

available_till = forms.TimeField(
required=False,
widget=TimeInput(),
)

advance = forms.IntegerField(
required=False,
)

"""Function to ensure that booking is done for future and check out is after check in"""
def clean(self):
cleaned_data = super().clean()
available_from = cleaned_data.get("available_from")
available_till = cleaned_data.get("available_till")
str_available_from = str(available_from)
str_available_till = str(available_till)
format = '%H:%M:%S'
if str_available_from != 'None':
try:
datetime.datetime.strptime(str_available_from, format).time()
except Exception:
raise ValidationError(
_('Wrong time entered.'),
code='Wrong time entered.',
)
if str_available_till != 'None':
try:
datetime.datetime.strptime(str_available_till, format).time()
except Exception:
raise ValidationError(
_('Wrong time entered.'),
code='Wrong time entered.',
)
if available_till is not None and available_from is not None:
if available_till <= available_from:
raise ValidationError(
"Available till should be after available from.", code='Available till after available from'
)

View .py

    if categories == []:
categories = ['Regular', 'Executive', 'Deluxe', 'King', 'Queen']
if capacities == []:
capacities = [1, 2, 3, 4]
if advance is None:
advance = 0
if available_from is None and available_till is None:
room_list = Room.objects.filter(
category__in=categories,
capacity__in=capacities,
advance__gte=advance
)
elif available_from is None:
room_list = Room.objects.filter(
category__in=categories,
available_till__gte=available_till,
capacity__in=capacities,
advance__gte=advance
)
elif available_till is None:
room_list = Room.objects.filter(
category__in=categories,
available_from__lte=available_from,
capacity__in=capacities,
advance__gte=advance
)
else:
room_list = Room.objects.filter(
category__in=categories,
available_from__lte=available_from,
available_till__gte=available_till,
capacity__in=capacities,
advance__gte=advance
)
return room_list

enter image description here

Link to GitHub repo

我写了代码,但我认为应该有更好的方法。

最佳答案

要同时查询多个事物,使用 |分隔它们的符号

varName = Room.objects.filter(category__in=categories) | Room.objects.filter(capacity__in=capacities) | Room.objects.filter(capacity__in=capacities)...

我最近在这样的搜索 View 中使用了它

object_list = Post.objects.filter(title__icontains=query) | Post.objects.filter(content__icontains=query) | Post.objects.filter(category__icontains=query) | Post.objects.filter(date_posted__icontains=query) | Post.objects.filter(author__username__icontains=query)

关于django - 如何在 Django 中创建多个过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74352339/

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