在我的视图和模板django中分割数据以匹配certian标准

时间:2016-08-22 12:01:52

标签: python django python-2.7

我希望能够根据描述将读取(读取Inspeciton_vals对象)数据分成多行(描述是我的维度模型中的对象)到目前为止,我还没有想出任何做法所以,任何帮助将不胜感激。

为了分解我有一个Inspeciton_vals模型,它有一个外键来标注每个维度将为我的图纸模型(对象sample_size)分配一定数量的inspection_vals,这个特定的一个是24,所以,我的inspection_vals表将由于它有两个尺寸,每个有24个,因此有48个。我正在考虑在我的维度模型中键入id以打破它,但我似乎无法找到一个可靠的解决方案。

这是我的views.py

@login_required 
def shipping(request, id): 
  sheet_data = Sheet.objects.get(pk=id) 
  work_order = sheet_data.work_order 
  customer_data = Customer.objects.get(id=sheet_data.customer_id) 
  customer_name = customer_data.customer_name 
  title_head = 'Shipping-%s' % sheet_data.work_order 
  complete_data = Sheet.objects.raw("""select s.id, d.id d_id, s.work_order, d.target, i.reading, d.description, i.serial_number from app_sheet s left join app_dimension d on s.id = d.sheet_id 
  left join app_inspection_vals i on d.id = i.dimension_id""") 


  for c_d in complete_data: 
    dim_description = Dimension.objects.filter(sheet_id=c_d.id).values_list('description', flat=True).distinct() 

    dim_id = Dimension.objects.filter(sheet_id=c_d.id)[:1] 
    for d_i in dim_id: 
      dim_data = Inspection_vals.objects.filter(dimension_id=d_i.id) 
      sample_size = dim_data   


  return render(request, 'app/shipping.html', 
  { 
     'work_order': work_order, 
     'sample_size': sample_size, 
     'customer_name': customer_name, 
     'title': title_head, 
     'complete_data': complete_data, 
     'dim_description': dim_description, 
  }) 

这是我的shipping.html

<div class="container"> 
  <div class="row"> 
      <div> 
        <table > 
          <thead> 
          <tr> 
            <th>Serial Number</th> 
            {% for ss in sample_size %} 
               <th>{{ ss.serial_number }}</th> 
            {% endfor %} 
          </tr> 
          </thead> 
          <tbody> 
             {% for desc in dim_description.all %} 
             <tr> 
                <th> {{ desc }}</th> 
                {% for r_c in complete_data %} 
                  <td> {{ r_c.reading }} </td> 
                {% endfor %}                 
             {% endfor %} 
             </tr> 
          </tbody> 
        </table> 
     </div>       
  </div> 
</div> 

这是我的模特

class Sheet(models.Model): 
  objects = SheetManager() 

  create_date = models.DateField() 
  updated_date = models.DateField() 
  customer_name = models.CharField(max_length=255) 
  part_number = models.CharField(max_length=255) 
  part_revision = models.CharField(max_length=255) 
  work_order = models.CharField(max_length=255) 
  purchase_order = models.CharField(max_length=255) 
  sample_size = models.IntegerField() 
  sample_scheme = models.CharField(max_length=255) 
  overide_scheme = models.IntegerField() 
  template = models.IntegerField() 
  sample_schem_percent = models.IntegerField() 
  critical_dimensions = models.IntegerField() 
  closed = models.IntegerField() 
  serial_index = models.CharField(max_length=255) 
  drawing_number = models.CharField(max_length=255) 
  drawing_revision = models.CharField(max_length=255) 
  heat_number = models.CharField(max_length=255) 
  note = models.CharField(max_length=255) 
  valc = models.CharField(max_length=255) 

class Dimension(models.Model): 
  description = models.CharField(max_length=255) 
  style = models.CharField(max_length=255) 
  created_at = models.DateField() 
  updated_at = models.DateField() 
  target = models.IntegerField() 
  upper_limit = models.IntegerField() 
  lower_limit = models.IntegerField() 
  inspection_tool = models.CharField(max_length=255) 
  critical = models.IntegerField() 
  units = models.CharField(max_length=255) 
  metric = models.CharField(max_length=255) 
  target_strings = models.CharField(max_length=255) 
  ref_dim_id = models.IntegerField() 
  nested_number = models.IntegerField() 

  met_upper = models.IntegerField() 
  met_lower = models.IntegerField() 
  valc = models.CharField(max_length=255) 
  sheet = models.ForeignKey(Sheet, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY) 

class Inspection_vals(models.Model): 
  created_at = models.DateField() 
  updated_at = models.DateField() 
  reading = models.IntegerField(null=True) 
  reading2 = models.IntegerField(null=True) 
  reading3 = models.IntegerField(null=True) 
  reading4 = models.IntegerField(null=True) 
  state = models.CharField(max_length=255) 
  state2 = models.CharField(max_length=255) 
  state3 = models.CharField(max_length=255) 
  state4 = models.CharField(max_length=255) 
  approved_by = models.CharField(max_length=255) 
  approved_at = models.DateField(null=True, blank=True) 
  dimension = models.ForeignKey(Dimension, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY) 
  serial_number = models.IntegerField(default=1) 

最后这是我现在看起来像的屏幕截图。

Intial

这是我希望它的样子 Here is what I want it to look like

此外,如果它有助于我在数据库中添加我的数据,以提供更深入的详细信息。

Data

1 个答案:

答案 0 :(得分:0)

基本上把它弄清楚我把两个列表变成了一个字典,然后迭代它们就可以得到我想要的结果。

complete_data = Sheet.objects.raw("""select s.id, d.id d_id, s.work_order, d.target, i.reading, d.description, i.serial_number from app_sheet s left join app_dimension d on s.id = d.sheet_id  left join app_inspection_vals i on d.id = i.dimension_id""") 

  key_list = [] 
  vals_list = [] 

  for xr in complete_data: 

    key_list.append(xr.description) 
    vals_list.append(xr.reading) 

  reading_desc = {} 
  for i in range(len(key_list)): 
    if key_list[i] in reading_desc: 
      reading_desc[key_list[i]].append(vals_list[i]) 
    else: 
      reading_desc[key_list[i]]=[vals_list[i]] 



<div class="container"> 
  <div class="row"> 
      <div> 
        <table > 
          <thead> 
          <tr> 
            <th>Serial Number</th> 
            {% for ss in sample_size %} 
               <th>{{ ss.serial_number }}</th> 
            {% endfor %} 
          </tr> 
          </thead> 
          <tbody> 
             {% for key, values in reading_desc.items %} 
             <tr> 
               <td>{{key}}</td> 
               {% for v in values %} 
                 <td>{{v}}</td> 
               {% endfor %} 
            </tr> 
             {% endfor %} 
          </tbody> 
        </table> 
     </div>       
  </div> 
</div>