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
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<input autocomplete="off"
|
||||
list="{{ widget.name }}-list"
|
||||
type="{{ widget.type }}"
|
||||
name="{{ widget.name }}"
|
||||
{% if widget.value != None %}value="{{ widget.value|stringformat:'s' }}"{% endif %}
|
||||
{% include "django/forms/widgets/attrs.html" %}>
|
||||
<datalist id="{{ widget.name }}-list">
|
||||
{% for option in widget.options %}<option value="{{ option }}"></option>{% endfor %}
|
||||
</datalist>
|
Loading…
Add table
Add a link
Reference in a new issue