update ordering, queryset

This commit is contained in:
2024-01-10 10:00:32 +00:00
parent fee676d0c5
commit d54a632d67
2 changed files with 20 additions and 4 deletions

View File

@@ -2,7 +2,6 @@ from django.contrib import admin, messages
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import ngettext from django.utils.translation import ngettext
from segno import helpers from segno import helpers
from .forms import ( from .forms import (
@@ -336,6 +335,8 @@ class WirefAdmin(admin.ModelAdmin):
"total", "total",
] ]
ordering = ["-wiref_id"]
def add_view(self, request, form_url="", extra_context=None): def add_view(self, request, form_url="", extra_context=None):
extra_context = extra_context or {} extra_context = extra_context or {}
extra_context["help_text"] = "Fette Schriften sind Pflichtfelder." extra_context["help_text"] = "Fette Schriften sind Pflichtfelder."

View File

@@ -1,5 +1,6 @@
from django import forms from django import forms
from django.core.validators import ValidationError from django.core.validators import ValidationError
from django.db.models import Count
from django.forms import DateInput from django.forms import DateInput
from members.models import Member from members.models import Member
@@ -312,6 +313,18 @@ class BillAdminForm(forms.ModelForm):
self.fields["bill_creator"].widget.can_change_related = False self.fields["bill_creator"].widget.can_change_related = False
self.fields["bill_creator"].widget.can_delete_related = False self.fields["bill_creator"].widget.can_delete_related = False
# delete wiref id from list if there are already 8 bills in wiref form.
qs = (
self.fields["wiref"]
.queryset.annotate(num_bills=Count("bill"))
.filter(num_bills__lt=8)
)
# delete wiref id from if there is a uploaded wiref file.
qs = qs.filter(file_field="")
self.fields["wiref"].queryset = qs.order_by("-wiref_id")
class ResolutionAdminForm(forms.ModelForm): class ResolutionAdminForm(forms.ModelForm):
total = forms.CharField() total = forms.CharField()
@@ -373,10 +386,12 @@ class WirefAdminForm(forms.ModelForm):
super().__init__(*args, **kwargs) # to get the self.fields set super().__init__(*args, **kwargs) # to get the self.fields set
wiref = kwargs.get("instance", None) wiref = kwargs.get("instance", None)
bills = Bill.objects.filter(wiref=wiref)
total = 0 total = 0
for elem in bills:
total += elem.amount if wiref is not None:
bills = Bill.objects.filter(wiref=wiref)
for elem in bills:
total += elem.amount
self.fields["total"].disabled = True self.fields["total"].disabled = True
self.fields["total"].initial = total self.fields["total"].initial = total