我正在学习django。我有一个名为customer的简单模型。这是我的模特:
class Year(models.Model):
year = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.year
def __str__(self):
return self.year
class Customer(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Product(models.Model):
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE)
quantity = models.CharField(max_length=255)
year = models.ForeignKey(Year, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.score
def __str__(self):
return self.score
并且查看是: 来自django.shortcuts导入渲染,get_object_or_404,重定向 来自.models导入客户,产品,年份
def home(request):
customers = Customer.objects.all()
years = Year.objects.all().values_list('year', flat=True).asc() # List of year name
rows = []
for year in years:
row = [year] + [None] * len(customers) # Row with year in first column, and the rest init with same size of customers list
for idx, customer in enumerate(customers):
quantities = customer.product_set.filter(year__year=year).valu e_list('quantity', flat=True) # filter product by year. That can return multiple product !!!
row[idx + 1] = ' ,'.join(quantities) # create a string of quantities
rows.append(row) # Add new row in our rows list
context = {'customers': customer,
'rows': rows}
return render(request, 'customer.html', context)
模板:
{% extends 'base.html' %}
{% block customer %}
<div class="container">
<h2>Players Table</h2>
<p>Customer with Product</p>
<table class="table">
<thead>
<tr>
<th>Year/Product</th>
{% for customer in customers %}
<th>{{ customer.name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
{% for cell in row %}
<th class="">{{cell}}</th>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
现在我希望在每一行的单元格中,这将是类并返回客户名称,就像
<th class='Customer Name 1'> 454</th>
行的每个单元格都将返回客户名称和产品。
请尽早帮助我。
答案 0 :(得分:2)
考虑模板中的显示格式,您需要使用如下的词典列表:
<强>视图强>
def home(request):
customers = Customer.objects.all()
years = Year.objects.all().values_list('year', flat=True).asc() # List of year name
result= []
for year in years:
row = {year: []} # Row with year as key
for idx, customer in enumerate(customers):
quantities = customer.product_set.filter(year__year=year).value_list('quantity', flat=True) # filter product by year. That can return multiple product !!!
row[year].append({customer: ' ,'.join(quantities)}) # {'2017': [{'A': '2 ,2 ,3'}]}
result.append(row) # Create list of dicts [{'2017': [{'A': '2 ,2 ,3'}, {'B': '2 ,2 ,3'}, {'C': '2 ,2 ,3'}]}, {'2016': [{'A': '2 ,2 ,3'}, {'B': '2 ,2 ,3'}, {'C': '2 ,2 ,3'}]}]
context = {'customers': customer, 'result': result}
return render(request, 'customer.html', context)
<强>模板:强>
<tbody>
{% for data in result %}
{% for year, cust_prod_data in data.items %}
<tr>
<th>{{year}}</th>
{% for cust_prod in cust_prod_data %}
{% for customer_name, prod_qty in cust_prod.items %}
<th class="{{customer_name}}">{{prod_qty}}</th>
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
{% endfor %}
</tbody>