Python 以django模型中存在的特定次数循环通过块
我有“事件”、“票”和“订单”的模型。当某人为特定事件订购票证时,该票证将按顺序保存。在Order model/table中,我有amount和其他字段,然后是quantity(以了解人员为特定事件实际选择的票证类别A和票证类别B的数量。订购后,我希望显示票证类别A和票证类别B的次数(根据用户为每个事件选择的数量)所以他们可以打印。我花了几个小时尝试了很多东西,并通过int循环(这在django中是不可能的)绕过下面的代码片段 模型 意见 和在票证打印模板中Python 以django模型中存在的特定次数循环通过块,python,django,Python,Django,我有“事件”、“票”和“订单”的模型。当某人为特定事件订购票证时,该票证将按顺序保存。在Order model/table中,我有amount和其他字段,然后是quantity(以了解人员为特定事件实际选择的票证类别A和票证类别B的数量。订购后,我希望显示票证类别A和票证类别B的次数(根据用户为每个事件选择的数量)所以他们可以打印。我花了几个小时尝试了很多东西,并通过int循环(这在django中是不可能的)绕过下面的代码片段 模型 意见 和在票证打印模板中 {% for item in mai
{% for item in mainList %}
{% for items in item %}
<p>{{ items }}</p>
{% endfor %}
{% endfor %}
一,
二,
三,
四,
五,
六,
一,
二,
三,
四,
五,
我认为你应该重构你的代码
不应该关心最大可用的Ticket,这是Ticket
模型的一个字段事件
- 每个
实例都应通过一对一关系(即订单
的外键来自订单
)与其所有票据相关票据
- 如果您想将所有相关的票证循环到您的订单上,那么只需在self.ticket\u set.all()中对票证执行
:
- 如果您想要总票数,您可以将其保存为订单模型上的一个字段,或者只需使用
self.ticket\u set.count()
class Event(models.Model):
name = models.CharField(max_length=64)
sales_start = models.DateTimeField(null=True, blank=True)
sales_end = models.DateTimeField(null=True, blank=True)
description = models.CharField(max_length=250, null=True, blank=True)
quantity = models.PositiveIntegerField(null=True)
class Order(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
event = models.ForeignKey(Event, null=True, blank=True)
transactionID = models.CharField(max_length=100, null=True, blank=True)
date = models.DateTimeField(auto_now_add=True, null=True, blank=True,
verbose_name='Order date')
status = models.CharField(max_length=100, null=True, blank=True)
def total_amount(self):
return sum(ticket.amount for ticket in self.ticket_set)
def ticket_type(self, t):
return self.ticket_set.filter(type=t)
class Ticket(models.Model):
order = models.ForeignKey(Order)
type = models.CharField(max_length=8, choices={(('A', 'Main stand'),
('B', 'Lower stand'))})
amount = models.FloatField()
使用这些模型,您现在可以轻松地将想要的对象传递到模板:
视图.py
from django.shortcuts import get_object_or_404, render
def view_order(request, transactionID):
order = get_object_or_404(Order, transactionID=transactionID)
class_a = order.ticket_set.filter(type='A')
class_b = order.ticket_set.filter(type='B')
context = {'class_a': class_a, 'class_b': class_b, 'id': transactionID}
return render(request, 'app_name/view_order.html', context)
app\u name/view\u order.html
Order details for transaction "{{ id }}"
<h3>Class A tickets:
{% for ticket in class_a %}
{{ ticket.amount }} <!-- Print whatever you'd like here, all Ticket fields are available -->
{% endfor %}
<h3>Class B tickets:
{% for ticket in class_b %}
{{ ticket.amount }} <!-- Print whatever you'd like here, all Ticket fields are available -->
{% endfor %}
交易{{id}的订单详细信息
A类门票:
{a类中的票证为%}
{{ticket.amount}}
{%endfor%}
B类门票:
{b类中的票证为%}
{{ticket.amount}}
{%endfor%}
以下是以更简单的方式重写视图的尝试:
from collections import defaultdict
def ticket_print(request, transactionID):
orders = Order.objects.filter(transactionID=transactionID)
ticket_class = defaultdict(list)
for order in orders:
for ticket in range(int(order.ticket.quantity)):
ticket_class[ticket.name].append(ticket)
return render_to_response(template,
{"tickets": ticket_class},
context_instance=RequestContext(request))
在模板中,票证应作为按票证类名分组的字典提供:
{% for ticketclass, ticketlist in tickets.iteritems %}
{% for ticket in ticketlist %}
print ticket
{% endfor %}
{% endfor %}
成功了,我就是这么做的 观点 模板
{% for item in mainList %}
{% for items in item %}
<div class="print">
<div class="ticketSmall" style="margin-left: 10%;">
<div class="ticketType"><h2>{{ items|title }}</h2></div>
<div class="imgHolder"><img src="{{ STATIC_URL }}{{ eventMemo.logo }}" width="270" height="300">
Print this Ticket and come with it to the venue
</div>
<div class="ticketInfo"><h2>{{ eventMemo|title }}</h2>
<ul>
<li>Venue:<b> {{ eventLoc|title }}</b></li>
<li>Date: <b>{{ eventDate|date }}</b></li>
<li>Time: <b>{{ eventDate|time }}</b></li>
<li>Ticket Purchased By: <strong>{{ user.first_name|title }} {{ user.last_name|title }}</strong></li>
<li>Ticket Type: <strong>{{ items|title }}</strong></li>
<li>Date of Purchase: <strong>{{ now }}</strong></li>
</ul>
</div>
</div>
</div>
{% endfor %}
{% endfor %}
{%for mainList%中的项]
{项目%中的项目为%0}
{{项目|标题}
打印这张票,然后带着它去会场
{{eventMemo | title}
- 地点:{eventLoc | title}
- 日期:{eventDate | Date}
- 时间:{eventDate | Time}
- 购票人:{{user.first|name | title}{{user.last|name | title}}
- 票证类型:{{items | title}
- 购买日期:{{now}
{%endfor%}
{%endfor%}
你能发布模型吗?不清楚“票证类别”在哪里。现在,随着模型的更新,我正在编写一个应用程序,我们不知道票证和活动的数量。它可能有四个!而且,活动不应该有“数量”,您可以知道特定活动的票务类别数量,而不是活动数量。我所说的数量是指可用票务。
Order details for transaction "{{ id }}"
<h3>Class A tickets:
{% for ticket in class_a %}
{{ ticket.amount }} <!-- Print whatever you'd like here, all Ticket fields are available -->
{% endfor %}
<h3>Class B tickets:
{% for ticket in class_b %}
{{ ticket.amount }} <!-- Print whatever you'd like here, all Ticket fields are available -->
{% endfor %}
from collections import defaultdict
def ticket_print(request, transactionID):
orders = Order.objects.filter(transactionID=transactionID)
ticket_class = defaultdict(list)
for order in orders:
for ticket in range(int(order.ticket.quantity)):
ticket_class[ticket.name].append(ticket)
return render_to_response(template,
{"tickets": ticket_class},
context_instance=RequestContext(request))
{% for ticketclass, ticketlist in tickets.iteritems %}
{% for ticket in ticketlist %}
print ticket
{% endfor %}
{% endfor %}
myList = []
mainList = []
counter = 1
ticketMemo = ''
for x in a:
eventMemo = x.event
ticketMemo = x.ticket
z = int(x.quantity)
for counter in range(counter, z+1):
myList.append(ticketMemo)
counter += 1
counter = 1
mainList.append(myList)
myList = []
context = {
'mainList': mainList,
'transactionID': transactionID,
'ticketMemo': ticketMemo,
'eventMemo': eventMemo,
'eventLoc':eventLoc,
'eventDate':eventDate,
'ticketClass': ticketClass,
}
return render_to_response(template,context, context_instance=RequestContext(request))
{% for item in mainList %}
{% for items in item %}
<div class="print">
<div class="ticketSmall" style="margin-left: 10%;">
<div class="ticketType"><h2>{{ items|title }}</h2></div>
<div class="imgHolder"><img src="{{ STATIC_URL }}{{ eventMemo.logo }}" width="270" height="300">
Print this Ticket and come with it to the venue
</div>
<div class="ticketInfo"><h2>{{ eventMemo|title }}</h2>
<ul>
<li>Venue:<b> {{ eventLoc|title }}</b></li>
<li>Date: <b>{{ eventDate|date }}</b></li>
<li>Time: <b>{{ eventDate|time }}</b></li>
<li>Ticket Purchased By: <strong>{{ user.first_name|title }} {{ user.last_name|title }}</strong></li>
<li>Ticket Type: <strong>{{ items|title }}</strong></li>
<li>Date of Purchase: <strong>{{ now }}</strong></li>
</ul>
</div>
</div>
</div>
{% endfor %}
{% endfor %}