Add category inheritance to view and template
This commit is contained in:
parent
a3273c13cf
commit
ea9d871e25
2 changed files with 14 additions and 5 deletions
|
@ -31,9 +31,7 @@
|
|||
|
||||
{% if categories %}
|
||||
<ul>
|
||||
{% for cat in categories %}
|
||||
<li>{{ cat }}</li>
|
||||
{% endfor %}
|
||||
{{ categories|unordered_list }}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
|
|
|
@ -9,11 +9,22 @@ from .models import Transaction, TransactionForm, Invoice, InvoiceForm, Category
|
|||
@login_required
|
||||
def index(request):
|
||||
_transactions = Transaction.objects.order_by("-date")[:5]
|
||||
_categories = Category.objects.order_by("name")
|
||||
_categories = Category.objects.filter(parent=None).order_by("name")
|
||||
|
||||
def _cat_list(cat):
|
||||
children = []
|
||||
for child in Category.objects.filter(parent=cat):
|
||||
children += _cat_list(child)
|
||||
if len(children) == 0: return cat,
|
||||
return cat, children
|
||||
|
||||
_cats = []
|
||||
for cat in _categories:
|
||||
_cats += _cat_list(cat)
|
||||
|
||||
context = {
|
||||
"transactions": _transactions,
|
||||
"categories": _categories,
|
||||
"categories": _cats,
|
||||
}
|
||||
return render(request, "main/index.html", context)
|
||||
|
||||
|
|
Loading…
Reference in a new issue