显示全部来自查询集而不是仅一个元素

时间:2019-05-15 04:01:28

标签: django python-3.x django-queryset

我正在尝试显示查询集中的所有项目,仅显示一个项目

下面是我的代码,该代码仅返回查询集special_list中的第一个元素:

views.py

try:
    try:
        specail_list = Special.objects.get(promotion=True, sp_code=promocode, start__lte=date_instance, minimum_stay__lte=nights, object_id=room_filter.id, end__gte=date_instance, is_active=True, member_deal=False)
        is_promo=True
    except:
        specail_list = Special.objects.filter(promotion=False, start__lte=date_instance, minimum_stay__lte=nights, object_id=room_filter.id, end__gte=date_instance, is_active=True, member_deal=False)
        is_promo = False

    min_stay_check_end = chek_in + datetime.timedelta(days=specail_list.minimum_stay - 1)
    min_stay_check_start = check_out - datetime.timedelta(days=specail_list.minimum_stay)

    if min_stay_check_end > (specail_list.end.date() + datetime.timedelta(days=1)) and (check_out + datetime.timedelta(days=1)) > specail_list.end.date():
        specail_list = None

    if min_stay_check_start < (specail_list.start.date() + datetime.timedelta(days=1)) and (chek_in < specail_list.start.date()  + datetime.timedelta(days=1)):
        specail_list = None

except:
    specail_list = None

try:
    for element in specail_list:
        if element.discount_type =="NIGHTS":
            if specials_nights < (element.minimum_stay * int(nights/element.minimum_stay)): 
                discount = element.nights_off_discount(Decimal(cost_of_rooms))
                print(discount)
                specials_nights += 1
             else:
                 discount = 0
         elif element.discount_type =="FLAT":
                 discount = Decimal(element.pay_days)
         else:
             discount = element.discount(1,Decimal(cost_of_rooms),adults,children,no_of_rooms)

             cost_of_rooms = Decimal(cost_of_rooms)-Decimal(discount)

             price_instance= math.ceil((Decimal(price_instance) * Decimal(ex.rate))*100)/100
                                cost_of_rooms = math.ceil((Decimal(cost_of_rooms) * Decimal(ex.rate))*100)/100

             discount_recived =Decimal(price_instance)-Decimal(cost_of_rooms)
             newli.append({'room': room_instance, 'display_order':room_filter.display_order, 'rm': room_filter, 'date': date_instance, 'quantity':av['quantity'], 'price':int(price_instance),'error':True,'discount':discount_recived,'sp_name':element.name,'sp_description':element.description,'sp_start':element.start,'sp_end':element.end,'count_sp':specail_list})









   discount_count.append({'room':room_instance,'normal_rate':price_instance,'discount':discount_recived})
   except Exception:                               
                            try:
                                price_instance = math.ceil((Decimal(price_instance) * Decimal(ex.rate))*100)/100
                                cost_of_rooms = math.ceil((Decimal(cost_of_rooms) * Decimal(ex.rate))*100)/100
                            except:
                               pass
                            discount_recived =Decimal(price_instance)-Decimal(cost_of_rooms)
                            newli.append({'room': room_instance, 'display_order':room_filter.display_order, 'rm': room_filter, 'date': date_instance, 'quantity':av['quantity'], 'price': int(price_instance),'error':True})
                            discount_count.append({'room':room_instance,'normal_rate':price_instance,'discount':discount_recived})


   newlist = sorted(newli, key=itemgetter('quantity'))

模板

{% if rooms.discount_percent > 0 or rooms.sp_name %}
{% for i in rooms.sp_name|slice:"2" %}  
    <div class="room-plan row ">

        <input id="roomid_{{rooms.rm.id}}" type="checkbox" class="room_id" name ="select_room" value="{{rooms.rm.id}}" >
        <span class="plan-title" id="sp_{{rooms.rm.id}}">{% if rooms.sp_name %}{{rooms.sp_name}}{% else %}{{rooms.discount_percent}}% Discount{% endif %}</span>
        <a class="link"  data-toggle="modal" data-target="#discount_{{rooms.rm.id}}"> Rate Details</a>

        <span class="cost" id="{{rooms.rm.id}}">
            {{current_symbol}}{{rooms.discountprice}}
        </span>
        <span class="per">per {{rooms.rm.rate_based_on}}</span>

    </div>
{% endfor %}
{% endif %}

在“我的模板”上返回两个项目,但是它们都显示了我的queryset中的第一个元素:

specail_list = Special.objects.filter(promotion=False, start__lte=date_instance, minimum_stay__lte=nights, object_id=room_filter.id, end__gte=date_instance, is_active=True, member_deal=False)

注意:我已经更新了代码,但仍无法正常工作,它没有显示模板specail_list中的任何项目

1 个答案:

答案 0 :(得分:1)

您编写的代码仅返回1个元素。如果要获取所有结果,请使用filter正确的第二个查询(嗯......没有[0]

当您收到多个退货商品时,必须更改代码。您不再处理一个元素而是一个托管列表。这些项目中的每一个都有.minimum_stay,它们可能彼此不同。因此,您需要遍历查询集以执行您想做的任何事情。

for element in special_list:
    # do whatever you need to do/compare with that element