Add autocomplete for transaction fields
Needs work on ordering by -count
This commit is contained in:
parent
951f157de9
commit
c7994114a1
2 changed files with 47 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
from category.models import Category
|
||||
from django.forms.widgets import TextInput
|
||||
from main.forms import NummiFileInput, NummiForm
|
||||
from statement.models import Statement
|
||||
|
||||
|
|
@ -26,6 +27,30 @@ class TransactionForm(NummiForm):
|
|||
super().__init__(*args, **kwargs)
|
||||
self.fields["category"].queryset = Category.objects.filter(user=_user)
|
||||
self.fields["statement"].queryset = Statement.objects.filter(user=_user)
|
||||
self.fields["name"].widget = DatalistInput(
|
||||
options=[
|
||||
t.name
|
||||
for t in _user.transaction_set.exclude(name=None)
|
||||
.order_by("name")
|
||||
.distinct("name")
|
||||
]
|
||||
)
|
||||
self.fields["trader"].widget = DatalistInput(
|
||||
options=[
|
||||
t.trader
|
||||
for t in _user.transaction_set.exclude(trader=None)
|
||||
.order_by("trader")
|
||||
.distinct("trader")
|
||||
]
|
||||
)
|
||||
self.fields["payment"].widget = DatalistInput(
|
||||
options=[
|
||||
t.payment
|
||||
for t in _user.transaction_set.exclude(payment=None)
|
||||
.order_by("payment")
|
||||
.distinct("payment")
|
||||
]
|
||||
)
|
||||
if _disable_statement:
|
||||
self.fields["statement"].disabled = True
|
||||
|
||||
|
|
@ -42,3 +67,16 @@ class InvoiceForm(NummiForm):
|
|||
widgets = {
|
||||
"file": NummiFileInput,
|
||||
}
|
||||
|
||||
|
||||
class DatalistInput(TextInput):
|
||||
template_name = "transaction/forms/widgets/datalist.html"
|
||||
|
||||
def __init__(self, *args, options=[]):
|
||||
self.options = options
|
||||
super().__init__(*args)
|
||||
|
||||
def get_context(self, *args):
|
||||
context = super().get_context(*args)
|
||||
context["widget"]["options"] = self.options
|
||||
return context
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue