James Behling
James Behling

Reputation: 45

Display data from multiple tables in one Template

I'm trying to create a dashboard for orders placed by a customer. I've got multiple tables, but the two I'm dealing with is "Order" and "Order_Detail". From the "Order" table, I need to display Customer, Status, and Required_date, from the "Order_Detail" table, I need Product. I'm able to access the data but I can figure out how to display all the records at the same time on one page.

models.py

    class Order(models.Model):
    STATUS = (
            ('New', 'New'),
            ('Ready For Fullfillment', 'Ready For Fullfillment'),
            ('Fullfilled Ready For Delivery', 'Fullfilled Ready For Delivery'),
            ('Out For Delivery', 'Out For Delivery'),
            ('Delivered', 'Delivered'),
            )
    order_date = models.DateTimeField(auto_now_add=True, null=True)
    required_date = models.DateTimeField(auto_now_add=True, null=True)
    status = models.CharField(max_length=200, null=True, choices=STATUS)
    note = models.CharField(max_length=1000, null=True, blank=True)
    customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
    
    def __str__(self):
        return self.customer.name

class Order_Detail(models.Model):
    orderNumber = models.ForeignKey(Order, null=True, on_delete=models.SET_NULL)
    product = models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
    pounds = models.IntegerField(null=True)
    ounces = models.IntegerField(null=True)
    container = models.ForeignKey(Container, null=True, on_delete=models.SET_NULL)
    lot_code = models.CharField(max_length=200, null=True, blank=True)
    note = models.CharField(max_length=1000, null=True, blank=True)

In my views.py, I'm playing with the following code:

    print('TEST2')
    o = Order.objects.all()
    o_notdelivered = Order.objects.exclude(status='Delivered')
    for e in o_notdelivered:
        print('Order ID:', e.id)
        order_detail_data = serializers.serialize("python", Order_Detail.objects.all().filter(orderNumber=e.id))
        print('order_detail_data:', order_detail_data)

With that code, I'm able to get the ORDER data and related ORDER_DETAIL data, but I can't figure out how to put the fields I need together and send it to my template. Please note that their will be multiple orders and order_details that will need to go to the template.

Basically, in the end, I want, on my dashboard a table that looks like this: |Customer|Product|Required Date|Status| |-|-|-|-| |C1|Product1|date here|New| |C2|Product3|date here|New|

Upvotes: 0

Views: 1545

Answers (2)

boyenec
boyenec

Reputation: 1617

use this query in your views Order_Detail.objects.all() and pass the context.

views.py

   order_details = Order_Detail.objects.all()
   context = {'order_details':order_details }
   ....your others code

then in your html:

   {{order_details.product}} #accessing data from your Order_Detail model

    {{order_details.orderNumber.status}}  #accessing data from your Order model via foreignkey



 
 
     

Upvotes: 1

Hatprob
Hatprob

Reputation: 116

In your template (.html file) you have to access this variables, for example, if you want to show the status of all packages, then, in the html file you should write something like:

{% for object in o %} <!-- Works like in python-->
            
           <h1>{{object.status}}</h1> 
           <!-- Do this for all other attributes-->
       
{% endfor %} <!-- Must close 'for loop'-->

Upvotes: 1

Related Questions