我有2个模型,客户和订单,订单通过foreignkey链接到客户,我的问题来自于将客户与多个订单相关联,在我的customer_list.html中,我这样做将名称链接到pk:
{% for customer in customer_list %}
....href="{%url 'order_app:order_detail' pk=customer.pk %}"> Customer Name..
{% endfor %}
我想在order_detail.html中显示该特定客户的所有订单,例如:
{% for order in customer.orders.all %}
<tr>
<td>{{order.pk}}</td>
<td>{{order.project_title}}</td>
<td>{{order.order_date}}</td>
<td>{{order.order_status}}</td>
<td>{{order.amount}}</td>
</tr>
{% endfor %}
这种方法不起作用,我似乎无法弄清楚如何解决这个问题 谢谢你的帮助
查看客户档案:
class CustomerListView(ListView):
model = Customer
template_name = 'order_app/customer_list.html'
查看订单明细:
class OrderDetailView(DetailView):
model = Order
模型
class Customer(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField(unique = True)
contact = models.CharField(max_length=20)
def __str__(self): # string representation of the object (e.g Haris instead of Order Object 1
return self.name
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='orders')
project_title = models.CharField(max_length=200)
order_date = models.DateTimeField(default=timezone.now())
amount = models.IntegerField()
order_status = models.CharField(max_length=50)
upload = models.FileField(upload_to = 'uploads/')
comment = models.TextField(blank=True)