fix choices didn't updated.

This commit is contained in:
2021-05-26 02:01:01 +00:00
parent f0e6b5676c
commit ee4982022b

View File

@@ -168,15 +168,20 @@ class PostSearchForm(forms.Form):
year_choices = [('', _('Alle'))]
month_choices = [('', _('Alle'))] + list(MONTHS.items())
try:
first_post = Post.objects.get_queryset().last()
if first_post:
year_of_first_post = first_post.public_date.year
years = range(year_of_first_post, timezone.now().date().year + 1)
year_choices += [(i, i) for i in years]
except:
pass
year = forms.ChoiceField(label="Jahr", choices=year_choices, required=False)
month = forms.ChoiceField(label="Monat", choices=month_choices, required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # to get the self.fields set
try:
first_post = Post.objects.get_queryset().last()
if first_post:
year_of_first_post = first_post.public_date.year
years = range(year_of_first_post, timezone.now().date().year + 1)
year_choices = [('', _('Alle'))] + [(i, i) for i in years]
self.fields['year'].choices = year_choices
except:
pass