嗨,我的代码工作正常:
if getattr(hotel_main, "X", 1):
hotels1 = hotels.filter(Q(X=True))
for hotel in hotels1:
if models.CalendarDay.objects.filter(hotel=hotel, date=date).count() == 0:
similar_venues.append(hotel)
我反复使用上面的代码来检查不同的条件,如Q(Y = True),Q(Y = True),Q(Z = True)
如果我可以根据条件过滤列表我可以摆脱重复的代码...我想要这样的东西: similar_venues.filter(Q(X = True))任何帮助请...
答案 0 :(得分:0)
如果我理解你的要求:
filter_on_x = [obj for obj in similar_venues if obj.X]
filter_on_y = [obj for obj in similar_venues if obj.Y]
依此类推所有X,Y,Z
答案 1 :(得分:0)
您可以在列表中写条件:
conditions = [ Q(Y=True),Q(Y=True),Q(Z=True) ]
if getattr(hotel_main, "X", 1):
q_date = Q( calendarday__date = date )
for q in conditions:
for hotel in hotels.filter( q_date & q).distinct():
similar_venues.append(hotel)