Saganie
Saganie

Reputation: 23

'Order' object is not iterable [Django]

At the very beginning I would note that I'm a beginner as hel :P I have two models in my django models.py and I want them to display on a page. The issue is that I got error about no possible iteration and I don't know why.

Also, I'm following the Crash Course from Youtube with changing some thing for my use :)

Could You please advice as I haven't found any useful tips on google?

Thanks!

models.py

from django.db import models

# Create your models here.
class Supplier(models.Model):
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.name

class Order(models.Model):
    STATUS = (
            ('Pending Order', 'Pending Order'),
            ('Pending PR', 'Pending PR'),
            ('Declined by SME', 'Declined by SME'),
            ('Declined by Finance', 'Declined by Finance'),
            ('Ordered', 'Ordered'),
            ('Canceled', 'Canceled'),
            ('Delivered', 'Delivered'),
            )
    product = models.CharField(max_length=200, null=True)
    link = models.CharField(max_length=400, null=True)
    supplier = models.ForeignKey(Supplier, null=True, on_delete=models.SET_NULL)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    status = models.CharField(max_length=200, null=True, default='Pending Order', choices=STATUS)
    amount = models.CharField(max_length=40, null=True)
    comment = models.CharField(max_length=400, null=True, blank=True)
    requester = models.CharField(max_length=40, null=True)

    def __str__(self):
        return self.product

views.py

from django.shortcuts import render
from .models import *

# Create your views here.

def home(request):
    return render(request, 'accounts/dashboard.html')

def products(request):
    return render(request, 'accounts/products.html')

def customer(request):
    return render(request, 'accounts/customer.html')

def orders(request):
    orders = Order.objects.all()
    customers = Supplier.objects.all()
    context = {'orders': orders, 'customers': customers}
    return render(request, 'accounts/orders.html', {'orders': Order})

And a part of my HTML which is only bootstrap and HTML

    <table class="table table-sm">
                <tr>
          <th>Supplier</th>
          <th>Product (Name)</th>
                    <th>Product (ID)</th>
                    <th>Date Orderd</th>
                    <th>Status</th>
          <th>Requester</th>
                    <th>Update</th>
                    <th>Remove</th>
                </tr>
          {% for order in orders %}
            <tr>
              <td>{{order.supplier}}</td>
              <td>{{order.product}}</td>
              <td>{{order.link}}</td>
              <td>{{order.date_created}}</td>
              <td>{{order.status}}</td>
              <td>{{order.requester}}</td>
          {% endfor %}
            </table>

Upvotes: 1

Views: 1238

Answers (1)

JLeno46
JLeno46

Reputation: 1269

def orders(request):
    orders = Order.objects.all()
    customers = Supplier.objects.all()
    context = {'orders': orders, 'customers': customers}
    return render(request, 'accounts/orders.html', context)

Try this. In render you're passing the Order class instead of your queryset orders

Upvotes: 3

Related Questions