Cipher
Cipher

Reputation: 2122

Add Extra Option Tag to Select Field - Django

I am using crispy forms to build my form. Now i want to add one more extra <option> tag with value in my timesheet_jobs field which is Foreign Key Field. Please help me to add one more extra <option> field in my <select> at the end after loading all the options.

forms.py

class ClockInForm(forms.ModelForm):

        class Meta:
            model = TimesheetEntry
            fields = ['timesheet_jobs', 'timesheet_clock_in_date', 'timesheet_clock_in_time']

        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

Upvotes: 1

Views: 553

Answers (1)

art
art

Reputation: 1412

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    timesheet_jobs = self.fields['timesheet_jobs']                                    
    timesheet_jobs.choices = list(timesheet_jobs.choices)
    timesheet_jobs.choices.append(tuple(('create_new_job', 'Create New Job')))

Upvotes: 1

Related Questions