2017-03-24 53 views
0

我正在学习django。我有一个名为customer的简单模型。这里是我的模型:如何通过使用数据获取有很多通过django?

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 

这里是我的客户的看法:

from django.shortcuts import render, get_object_or_404, redirect 
from .models import Customer, Product, Year 


# Create your views here. 
def home(request): 
    customer = Customer.objects.all 
    product = Product.objects.all 

    year = Year.objects.all().prefetch_related('product_set') 

    context = {'customers': customer, 
       'years': year, 
       'products': product 
       } 
    return render(request, 'customer.html', context) 

这里是我的customer.html

{% 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 cutomers %} 
       <th>{{ customer.name }}</th> 
      {% endfor %} 
     </tr> 
    </thead> 
     <tbody> 


{#  <tr>#} 
{#   <th>2011</th>#} 
{#   <th>633</th>#} 
{#   <th>424</th>#} 
{#  </tr>#} 
{#  <tr>#} 
{#   <th>2012</th>#} 
{#   <th>353</th>#} 
{#   <th>746</th>#} 
{#  </tr>#} 
     </tbody> 
    </table> 
</div> 

{% endblock customer %} 

现在我必须产生表行其中将包含一年,客户和产品每年通过客户。 因此,如何获取包含年,年客户产品数据的每一行。

更简单, 年有很多客户和客户有一个产品思想的一年。

如何生成这个。请帮帮我。

回答

1

您的上下文中不需要产品或年份。您可以使用模型之间的关系来获得这样的信息:

views.py

from django.shortcuts import render, get_object_or_404, redirect 
from .models import Customer, Product, Year 

# Create your views here. 
def home(request): 
    customer = Customer.objects.all 
    context = {'customers': customer} 
    return render(request, 'customer.html', context) 

现在在你的模板,你可以得到:

All product of a customer, with year by product 

{% for customer in customers %} 
    Customer : {{ customer }}<br> 
    {% for product in customer.product_set.all %} 
     Product : {{ product }}/Year :{{ product.year }}<br> 
    {% endfor %} 
{% endfor %} 

但我不喜欢把太多模板中的逻辑。 我建议你在你的视图中创建表格的数据,在你的模型中更好。然后用这些数据生成你的html表格。

总结你需要的功能: 可以取代所有()与滤波器()

products = customer.product_set.all() # give you all product of a customer 

更新后的注解:

Views.py

from django.shortcuts import render, get_object_or_404, redirect 
from .models import Customer, Product, Year 

# Create your views here. 
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>cell</th> 
       {% endfor %} 
      </tr> 
     {% endfor %} 
     </tbody> 
    </table> 
</div> 

我认为这可以帮助您了解如何解决您的问题。我不测试这个代码,所以也许有错误。这并不完美!

+0

感谢您的回复。但是我必须生成一个2D表格,其中一列只显示一年(不重复),其他列包含产品数据。 –

+0

你能告诉我输出欲望吗? – Wilfried

+0

输出将返回包含三列的表格: 1.第一列<<全年(不重复) 2.第二列<<第一顾客的产品数据 3.第三列<<第二顾客的产品数据 您可以看到customer.html –