Следующие разделы кода явно отсутствуют.
Проблема, с которой я столкнулся, заключается в том, что подэлементы не отображаются в шаблоне. Кажется, они не привязываются к основному элементу во встроенном наборе форм с помощью кода item.subItems.
Как отобразить подобные связанные поля во встроенном наборе форм? Мне не нужно иметь возможность редактировать подэлементы.
Код: Выделить всё
view.py
accountQuantityForm = AccountQuantityFormSet(instance=account)
# Get items linked to an Account instance
items = Item.objects.filter(accountquantity__account=account).distinct()
for item in items:
subItems = SubItem.objects.filter(item=item)
# Add the sub items to the item object for display
# item.subItems doesn't exist yet, we are creating it and adding the queryset of subItems to it.
item.subItems = subItems
# apply the filtered items queryset for display in accountQuantityForm
for form in accountQuantityForm.forms:
form.fields['item'].queryset = items
context = {
'accountQuantityForm':accountQuantityForm,
}
Код: Выделить всё
template.html
{% for form in accountQuantityForm %}
Display form items here
{% if form.instance.item.subItems.exists %}
{{form.instance.item.subItems}}
This is where I would insert the sub items below the main item in the inline formset.
{% endif %}
{% endfor %}
Подробнее здесь: https://stackoverflow.com/questions/798 ... -in-django
Мобильная версия