how to show foreign key value instead of its ID

喜欢而已 提交于 2019-12-24 08:04:51

问题


i'm try to display foreign key value , but it instead return the id , in template

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)

this is my 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 %}

i expected to display the product name but it instead displayed the ID of the product thanks for your answer

来源:https://stackoverflow.com/questions/57799014/how-to-show-foreign-key-value-instead-of-its-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!