django:通过 view.py 中的向后关系检索对象,而不是模板

发布于 2025-01-06 04:52:53 字数 1053 浏览 1 评论 0原文

刚开始用 python 编程,我遇到了以下问题。我有一个显示供应商详细信息的模板,每个供应商都有员工,在模板页面上,我想显示员工的姓名。我知道如何在模板中执行此操作,但是如何在视图中执行此操作?

模型:

class Supplier(models.Model):
    co_name = models.CharField(max_length=100)
    co_city = models.CharField(max_length=100)
    co_state = models.CharField(max_length=2)

class Supplieremployees(models.Model):
    supplier = models.ForeignKey(supplier)
    expe_fname = models.CharField(max_length=50)

视图:

def supplier_detail(request, supplier_id):
    s = get_object_or_404(Supplier, pk=supplier_id)
    **test = s.supplieremployees_set.all()**
    return render_to_response('suppliersdb/supplier_detail.html', {'supplier': s})

模板:

...i dont want to use this way, how do i translate this into the view?
{% for supplieremployees in supplier.supplieremployees_set.all %}
    <li>IT Focal: {{ supplieremployees.expe_fname }}</li>
{% endfor %}

**TEST: {{ test.expe_fname }}**

{{ test.expe_fname }} 没有显示任何内容

Just starting out to program in python and I'm having the following issue. I have a template that shows details on a supplier, each supplier has employees and on the template page, I want to show the names of the employees. I know how to do it in the template, but how do you do that in the view?

MODELS:

class Supplier(models.Model):
    co_name = models.CharField(max_length=100)
    co_city = models.CharField(max_length=100)
    co_state = models.CharField(max_length=2)

class Supplieremployees(models.Model):
    supplier = models.ForeignKey(supplier)
    expe_fname = models.CharField(max_length=50)

VIEWS:

def supplier_detail(request, supplier_id):
    s = get_object_or_404(Supplier, pk=supplier_id)
    **test = s.supplieremployees_set.all()**
    return render_to_response('suppliersdb/supplier_detail.html', {'supplier': s})

TEMPLATE:

...i dont want to use this way, how do i translate this into the view?
{% for supplieremployees in supplier.supplieremployees_set.all %}
    <li>IT Focal: {{ supplieremployees.expe_fname }}</li>
{% endfor %}

**TEST: {{ test.expe_fname }}**

nothing shows up for {{ test.expe_fname }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时常饿 2025-01-13 04:52:53
def supplier_detail(request, supplier_id):
    s = get_object_or_404(Supplier, pk=supplier_id)
    for employee in s.supplieremployees_set.all():
        print employee.expe_fname
    return render_to_response('suppliersdb/supplier_detail.html', {'supplier': s})

来自文档

def supplier_detail(request, supplier_id):
    s = get_object_or_404(Supplier, pk=supplier_id)
    for employee in s.supplieremployees_set.all():
        print employee.expe_fname
    return render_to_response('suppliersdb/supplier_detail.html', {'supplier': s})

from the docs

何以笙箫默 2025-01-13 04:52:53

Supplier 对象将具有可访问反向关系的 supplieremployees_set 属性:

employees = s.supplieremployees_set

您还可以向 ForeignKey< 提供 lated_name 参数/code> 并使用它:

class Supplieremployees(models.Model):
    supplier = models.ForeignKey(Supplier, related_name='employees')

employees = s.employees

A Supplier object will have a supplieremployees_set property that can access the reverse relation:

employees = s.supplieremployees_set

You can also supply a related_name argument to the ForeignKey and use that:

class Supplieremployees(models.Model):
    supplier = models.ForeignKey(Supplier, related_name='employees')

employees = s.employees
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文