form.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from .models import DiseaseType
  2. from django import forms
  3. from django.forms import ModelForm, Form
  4. from django.forms import IntegerField, CharField, ChoiceField, TextInput, ModelChoiceField, ModelMultipleChoiceField
  5. from django.utils import timezone
  6. current_year = timezone.now().year
  7. # https://stackoverflow.com/questions/5601425/django-and-modelform-how-to-change-integerfield-to-dropdown-box?answertab=scoredesc#tab-top
  8. YEAR_CHOICES = [(x,str(x)) for x in range(1949, current_year + 1 )]
  9. END_YEAR_CHOICES = [(x,str(x)) for x in range(current_year, 1948, -1 )]
  10. class PolicyFormSearch(Form):
  11. def __init__(self, *args, **kwargs):
  12. initial = kwargs.get("initial", {})
  13. initial["begin"] = "1950"
  14. kwargs["initial"] = initial
  15. super().__init__(*args, **kwargs)
  16. file_name = CharField(required=False, label="政策文件标题")
  17. department = CharField(required=False, label="发文部门")
  18. # widget=forms.CheckboxSelectMultiple,
  19. # 年份下拉选择:
  20. begin = IntegerField(required=False, #choices=YEAR_CHOICES,
  21. widget=forms.NumberInput(
  22. attrs={
  23. 'min':1949,'max': 3000,'type': 'number',
  24. 'filter_field': 'year',
  25. 'filter_method': '__gte',
  26. }
  27. ),
  28. label ="发布年份从")
  29. end = IntegerField(required=False, #choices=END_YEAR_CHOICES,
  30. widget=forms.NumberInput(
  31. attrs={
  32. 'min':1949,'max': 3000,'type': 'number',
  33. 'filter_field': 'year',
  34. 'filter_method': '__lt',
  35. }
  36. ),
  37. label='至', initial=current_year)
  38. disease_types = ModelChoiceField(queryset=DiseaseType.objects.all(), required=False, widget=forms.CheckboxSelectMultiple, label='涉及传染病')