Snowman08 Snowman08 - 3 months ago 4
Python Question

Spliting up data to match a certian criteria in my view and template django

I want to be able to split up my reading(reading is Inspeciton_vals object) data into multiple rows based off of a description(description is a object from my dimension model) So far I haven't figured out any way of doing so, any help would be greatly appreciated.

To break it down I have a Inspeciton_vals model that has a foreign key to dimension each dimension will have a determined amount of inspection_vals assigned for my sheet model (object sample_size) for this particular one it is 24 so, my inspection_vals table will have 48 due to it having two dimensions with 24 each. I was thinking about keying off the id in my dimension model to break it down but, I cant seem to figure out a solid solution.

Here is my 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,
})


here is my 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>


here are my models

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)


finally here is my screen shot of what it looks like now.

Intial

Here is what I would like it to look like
Here is what I want it to look like

Also if it helps I attached what my data looks like in my db to give more in depth detail.

Data

Answer

Figured it out basically I turned two list into a dictionary and just iterate over them to give me the results I wanted.

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>