【发布时间】:2017-11-06 20:18:01
【问题描述】:
我对 django 很陌生,所以如果有一个明显的答案,我深表歉意。
假设你有以下三个模型:
models.py
class Customer(models.Model):
name = models.CharField()
slug = models.SlugField()
class Product(models.Model):
plu = models.Charfield()
description = models.Charfield()
class Template(models.Model):
customer = models.ForeignKey(Customer)
product = models.ForeignKey(Product)
price = models.DecimalField()
内联表单集看起来像:
TemplateFormSet = inlineformset_factory(Customer, Template, extra=0,
fk_name='customer', fields=('price'))
是否可以向后跟随模板表单集的产品外键,以便您可以在同一个表中显示 plu 和描述字段?
例如这样的:
<table>
<tbody>
{% for obj in customer.template_set.all %}
<tr>
<td>{{ obj.product.plu }}</td>
<td>{{ obj.product.description }}</td>
<td>{% render_field formset.form.price class="form-control form-control-sm" %}</td>
</tr>
{% endfor %}
</tbody>
</table>
表单集的字段与上面的 html 一起出现,但来自绑定表单实例的数据没有出现,我无法通过编辑空字段来保存。
我也在下面尝试过,但是每个对象都会重复每个表单集(对于 x 个表单集,有 x*x 行):
<tbody>
{% for obj in customer.template_set.all %}
{% for form in formset %}
<tr>
<td>{{ obj.product.plu }}</td>
<td>{{ obj.product.description }}</td>
<td>{% render_field form.price class="form-control" %}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
Basically I'm trying to go from the top portion of the image to the bottom
【问题讨论】:
标签: python django python-3.x