Created
June 1, 2021 06:47
-
-
Save jainal09/eb6469baaa56e92e4d920b5ccaa5c120 to your computer and use it in GitHub Desktop.
Django Forms Dynamic Choice Field Class Based Views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
class UploadDocumentForm(forms.Form): | |
def __init__(self, company, filling_type, financials_types, *args, **kwargs): | |
super(UploadDocumentForm, self).__init__(*args, **kwargs) | |
self.fields["company"] = forms.ChoiceField(choices=company) | |
self.fields["financials_type"] = forms.ChoiceField(choices=filling_type) | |
self.fields["filling_types"] = forms.ChoiceField(choices=financials_types) | |
file = forms.FileField(widget=forms.FileInput( | |
attrs={ | |
'class': 'sr-only', | |
'data-upload-target': 'input', | |
'data-action': 'change->upload#selected', | |
'accept': 'application/pdf', | |
'multiple': False | |
}), | |
label='Choose file from your device', | |
required=True) | |
date = forms.DateField( | |
widget=forms.DateInput(attrs={'id': 'date-picker-year'}) | |
) | |
company = forms.ChoiceField() | |
financials_type = forms.ChoiceField() | |
filling_types = forms.ChoiceField() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateDocument(LoginRequiredMixin, FormView): | |
template_name = 'home/upload.html' | |
form_class = UploadDocumentForm | |
def get_form_kwargs(self): | |
kwargs = super().get_form_kwargs() | |
companies = list() | |
for company_obj in Company.objects.all(): | |
companies.append((company_obj.id, company_obj.name)) | |
fin_types = list() | |
for financials_type_obj in FilingType.objects.all(): | |
fin_types.append((financials_type_obj.id, financials_type_obj.code)) | |
fil_types = list() | |
for filling_type_obj in FinancialsType.objects.all(): | |
fil_types.append((filling_type_obj.id, filling_type_obj.code)) | |
kwargs['company'] = companies | |
kwargs['filling_type'] = fin_types | |
kwargs['financials_types'] = fil_types | |
return kwargs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment