3
我有一個簡單的InventoryItems和Categories設置。我有一個InventoryItems的formset,但是想分割基於FK Category的項目,我不需要或不需要內聯表單集。將django組合成基於外鍵的組合
簡體版的我有什麼
class Category(models.Model):
name = models.CharField(max_length=255)
inventory = models.BooleanField(default=False)
class Inventory(models.Model):
name = models.CharField(max_length=255)
quantity = models.IntegerField()
category = models.ForeignKey(Category)
def viewOfForm(request):
categories = Category.objects.filter(inventory=True)
InventoryFormset = modelformset_factory(Inventory, can_delete=True, extra=1)
formset = InventoryFormset(request.POST or None, queryset=Inventory.objects.filter(category__inventory=True))
return render_to_response('template.html', locals())
我想什麼模板做
{% for category in categories %}
<fieldset class="collapse">
<h2>{{ category.name }}</h2>
{% for form in formset %}
{% if form.category == category %}
{{ form }}
{% endif %}
{% endfor %}
</fieldset>
{% endfor %}
不知道我怎麼錯過了使用form.instance前的能力。謝謝。 – 2012-02-26 09:03:59