django 位于/customer/1/的属性错误“customer”对象没有属性“order_set”

fquxozlt  于 2023-01-31  发布在  Go
关注(0)|答案(3)|浏览(120)

我试图访问每个客户的订单,我这样做:

def Customer(request, pk):
        Customer = customer.objects.get(id=pk)
        orders = Customer.order_set.all()
        order_count = orders.count()

        context = {
            'orders': orders,
            'customer': Customer,
            'order_count': order_count,
        }
        return render(request, 'Inventory_Management/customer.html', context)

在youtube上,该名男子说,您可以通过使用订单模型名称(在我的情况下是“order”)访问与客户相关的订单,然后添加_set,我这样做了,当我尝试查看客户时,现在我得到这个错误:/customer/1/中的属性错误“客户”对象没有属性“order_set”。
相关模型

class order(models.Model):
        STATUS = (
            ('Pending', 'Pending'),
            ('Out for delivery', 'Out for delivery'),
            ('Delivered', 'Delivered'),
        )
        order_head = models.ForeignKey(order_header, blank=False, null=True, on_delete=models.SET_NULL)
        items = models.ForeignKey(item, blank=False, null=True, on_delete=models.SET_NULL)
        Quantity = models.CharField(max_length=100)
        date_created = models.DateTimeField(auto_now_add=True, null=True)
        total = models.CharField(max_length=100)
        status = models.CharField(max_length=200, null=True, choices=STATUS)

        def __str__(self):
            return 'Order Customer: {self.order_head.Buyer}'.format(self=self)

    class customer(models.Model):
        name = models.CharField(max_length=12, blank=False)
        phone = models.CharField(max_length=12, blank=False)
        email = models.CharField(max_length=50, blank=False)
        date_created = models.DateTimeField(auto_now_add=True, null=True)

        def __str__(self):
            return self.name

相关视图

def Customer(request, pk):
        Customer = customer.objects.get(id=pk)
        orders = Customer.order_set.all()
        order_count = orders.count()

        context = {
            'orders': orders,
            'customer': Customer,
            'order_count': order_count,
        }
        return render(request, 'Inventory_Management/customer.html', context)

相关网址

path('customer/<str:pk>/', Customer, name="customer"),

相关html

{% for customer in customers %}
    <tr>
      <td><a class="btn btn-sm btn-info" href="{% url 'customer' customer.id %}">View</a></td>
      <td>{{customer.name}}</td>
      <td>{{customer.phone}}</td>
    </tr>
  {% endfor %}

我不知道这个查询是不是错了,但在youtube视频中,它是有效的。

k0pti3hp

k0pti3hp1#

url.py文件中使用views.Customer,而不仅仅是客户。
path('customer/<str:pk>/', views.Customer, name="customer")

7eumitmz

7eumitmz2#

试试这个..

def customer(request, pk):
    customer = Customer.objects.get(id=pk)
    orders = customer.order_set.all()
    orders_count = orders.count()
    context = {'customer':customer, 'orders':orders, 'orders_count':orders_count}
    return render(request, 'accounts/customer.html',context)
a0zr77ik

a0zr77ik3#

try with this 
it's work for me 

def Customer(request, pk):
        customer = Customer.objects.get(id=pk)
        orders = Customer.objects.all()
        myFilter = Customer(request.GET,queryset = orders)
        orders = myFilter.qs
        order_count = orders.count()

        context = {
            'orders': orders,
            'customer': customer,
            'order_count': order_count,
        }
        return render(request, 'Inventory_Management/customer.html', context)

相关问题