我试图访问每个客户的订单,我这样做:
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视频中,它是有效的。
3条答案
按热度按时间k0pti3hp1#
在
url.py
文件中使用views.Customer
,而不仅仅是客户。path('customer/<str:pk>/', views.Customer, name="customer")
7eumitmz2#
试试这个..
a0zr77ik3#