我尝试显示外键值,但它会返回模板中的id
models.py
class Product(models.Model):
product_name = models.CharField(unique=True, max_length=50)
pass
def __str__(self):
return self.product_name
class Order(models.Model):
id = models.AutoField(primary_key = True)
products = models.ManyToManyField(Product ,through='ProductOrder')
pass
def __str__(self):
return str(self.products.all())
class ProductOrder(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
ordering = models.ForeignKey(Order,
on_delete=models.CASCADE,blank=True,null=True)
pass
def __str__(self):
return str(self.product)
这是我的views.py
class ListOfOrder(ListView):
template_name = 'order/product_group.html'
context_object_name = 'productss'
def get_queryset(self):
return ProductOrder.objects.all()
template.html
{% for product in productss %}
<tr class="">
<td style="text-align: center;">{{ product.product }}</td>
{% endfor %}
我希望显示产品名称,但会显示产品ID 谢谢你的回答