我有三种模式:商业,优惠和OfferPlan: 业务:
class OfferPlan(models.Model):
WEEKDAYS = [
(1, _("Monday")),
(2, _("Tuesday")),
(3, _("Wednesday")),
(4, _("Thursday")),
(5, _("Friday")),
(6, _("Saturday")),
(7, _("Sunday")),
]
offer = models.ForeignKey(Offers, related_name="business_offer_plan",
on_delete=models.CASCADE)
weekday = models.IntegerField(
choices=WEEKDAYS,
)
from_hour = models.TimeField()
to_hour = models.TimeField()
信息:
class SearchListView(ListView):
template_name = 'search/search.html'
model = Business
def get_queryset(self):
# queryset = Business.objects.filter(business_address__city=AppLocations.objects.first().city)
if 'city' in self.request.GET:
queryset = Business.objects.filter(business_address__city=self.request.GET.get('city'))
if 'category' in self.request.GET:
queryset = queryset.filter(category__code=self.request.GET.get('category'))
# if 'date' not in self.request.GET:
# queryset = B
raise
return queryset
OfferPlan:
queryset.filter(business_offer__business_offer_plan__weekday=1).annotate(count_entry=Count('business_offer__business_offer_plan__weekday')).filter(count_entry__gt=1)
我有一个ListView,它根据城市,类别等不同的参数搜索商家。我现在想要在工作日搜索,说明星期一开放的商家将被显示,哪些不会被显示在那天。工作日信息存储在OfferPlan中,当天OfferPlan表中的商品可能有多个时间,但我想查询(过滤,排除)在该工作日编号上只有一个条目的商家。
这是我的ListView:
queryset.filter(business_offer__business_offer_plan__weekday=1).annotate(count_entry=Count('business_offer__business_offer_plan__weekday')).filter(count_entry__gte=1)
怎么可能这样呢?还研究了Array#[]
但无法弄清楚。
由于
在网络上进行了更多的研究后,我发现这是如何实现的,但需要从其他Django爱好者那里确定它是正确的。
>>> import itertools
>>> from collections import Counter
>>> c = Counter()
>>> for word in X:
words = word.split(' ')
words = [w for w in words if w in Y]
c.update(words)
>>> c
Counter({'xyz': 3, 'abc': 3, 'def': 3, 'tuv': 1})
杰斐逊的解决方案被标记为正确的答案,因为它提供了更多的见解,关于哪个查询很快以及我之前的更新有什么错误,所以这是我们都同意的正确解决方案:
<select onchange="callfun(this.value);">
<option>...Select Paragraph...</option>
<option value="paragraph1">Paragraph 1 </option>
<option value="paragraph2">Paragraph 2 </option>
<option value="paragraph3">Paragraph 3 </option>
<option value="paragraph4">Paragraph 4 </option>
</select>
答案 0 :(得分:1)
这里没有条件表达式 - 你的注释太复杂了。你只需要一个额外的过滤器。
queryset.filter(business_offer__business_offer_plan__weekday=self.request.GET['weekday'])
答案 1 :(得分:1)
def get_query(weekday):
businesses = Business.objects.filter(business_offer__in=Offers.objects.filter(
business_offer_plan__in=OfferPlan.objects.filter(weekday=weekday))).distinct()
return businesses
有一个沉重的查询,但它确实有效。