简体   繁体   English

Django 来自模板的外键值

[英]Django foreign key value from template

I've looked through other answers to similar questions but I can't seem to get it to work.我已经查看了类似问题的其他答案,但我似乎无法让它发挥作用。 here is my model structure:这是我的 model 结构:

models.py

class Seller(models.Model):
    seller_full_name = models.CharField(max_length=50)
    seller_listing_name = models.CharField(max_length=50) <-- I want to display this in template
    email = models.EmailField(max_length=50)
    date_added = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.seller_listing_name

# Create your models here.
class Product(models.Model):
    ...
    seller = models.ForeignKey(Seller, on_delete=models.CASCADE, null=True, blank=True)
    ...

    def __str__(self):
        return self.product_name

In my view I get a query set of products在我看来,我得到了一组查询产品

views.py

def store(request):
    products = Product.objects.values().order_by('?') # shuffle order
    args = {'products': products}
    return render(request, 'store/products-page.html', args)

In the template I iterate through products and build cards that display item details.在模板中,我遍历产品并构建显示项目详细信息的卡片。 I want to also display the listing name of the seller for each product on each card.我还想在每张卡片上显示每个产品的卖家列表名称。 I cannot seem to get the foreign key statement correct in the view because it renders empty where the sellers name should be.我似乎无法在视图中获得正确的外键语句,因为它在卖家名称应该出现的地方呈现为空。

products-page.html

{% for product in products %}
<div class="col-lg-3 col-md-6 mb-4">
    ...
        <p>
            // {{ product.seller.seller_listing_name }} // <-- display sellers name here
        </p> 
        <h4 class="card-title">
            <a href="#">{{ product.product_name }}</a>
        </h4>
        <p>#{{ product.product_id }}</p>
        <p>{{ product.description }}</p>
        <h5>${{ product.price | floatformat:2 }}</h5>
    ...
</div>
{% endfor %}

I've been messing with this for awhile and I can't seem to get it so any help would be awesome!我已经弄乱了一段时间,但似乎无法理解,所以任何帮助都会很棒!

EDIT Some links I've looked at:编辑我看过的一些链接:

Django: How to get data connected by ForeignKey via Template? Django:如何通过模板获取外键连接的数据?

Django foreign key relation in template Django 模板中的外键关系

How to access foreign key table's data in Django templates? Django模板中如何访问外键表的数据?

SOLUTION Thanks to the comments below I didn't realize that I was converting the products to a dictionary using.order_by('?') - This was resulting in a data structure that didn't keep my relationships so I wasn't able to access my FK.解决方案由于下面的评论,我没有意识到我正在使用 .order_by('?') 将产品转换为字典 - 这导致数据结构无法保持我的关系,所以我无法访问我的 FK。

views.py视图.py

def store(request):
    products = Product.objects.order_by('?') # REMOVE .values()
    args = {'products': products}
    return render(request, 'store/products-page.html', args)

products-page.html产品页面.html

{% for product in products %}
<div class="col-lg-3 col-md-6 mb-4">
    ...
        <p>
            // {{ product.seller.seller_listing_name }} // <-- display sellers name here
        </p> 
        <h4 class="card-title">
            <a href="#">{{ product.product_name }}</a>
        </h4>
        <p>#{{ product.product_id }}</p>
        <p>{{ product.description }}</p>
        <h5>${{ product.price | floatformat:2 }}</h5>
    ...
</div>
{% endfor %}

In case of ForeignKey you need to add something like如果是 ForeignKey,您需要添加类似

{% for product in products %}
<div class="col-lg-3 col-md-6 mb-4">
    [...]
        {% for seller in product.seller_set.all %}
        <p>
            // {{ seller.seller_listing_name }} // <-- display sellers name here
        </p> 
        {% endfor %}
        <h4 class="card-title">
            <a href="#">{{ product.product_name }}</a>
        </h4>
    [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM