我是Django和Python的新手,我用它来创建酒店预订网站。
我正在尝试取出与酒店相关的所有房间 使用返回的对象我试图在返回的每个房间添加额外的属性。 该属性将等于变量。
示例:
rooms = Room.object.filter(hotel=thehotel)
// for each room in rooms
Roomsavailable = Room.totalrooms
Roomsleft = RoomsAvailable - RoomsBooked
//attach roomsleft onto each room object
room.spaceleft = Roomsleft
任何帮助都会受到赞赏,希望我已经解释好了。
答案 0 :(得分:0)
我似乎通过盯着邮政编码问题找到了自己的答案。
无论如何,对于可能偶然发现此事的人来说:
rooms = Room.objects.filter(hotel= thehotel) *// thehotel refers to an ID*
for room in rooms:
room.spaceleft = 0 //setting the additional attribute
context = {'rooms':rooms}
*//passing the context to the template.*
return render(request, 'HotelApp/hoteldetails.html', context)
然后在模板中:
{% for room in rooms %}
<p> Rooms Left: {{room.spaceleft}} </p> // using the additional attribute
{% endfor %}