【问题标题】:Add Extra Option Tag to Select Field - Django为选择字段添加额外的选项标签 - Django
【发布时间】:2019-01-16 08:07:05
【问题描述】:

我正在使用清晰的表单来构建我的表单。现在我想在我的timesheet_jobs 字段中添加一个额外的<option> 标记,该字段是外键字段。请帮我在加载完所有选项后在我的<select> 中添加一个额外的<option> 字段。

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)

【问题讨论】:

    标签: django django-models django-forms django-templates django-views


    【解决方案1】:
    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')))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2014-01-19
      相关资源
      最近更新 更多