django - 为选择字段添加额外的选项标签 - Django
问题描述
我正在使用酥脆的表格来构建我的表格。现在我想在我的字段中添加一个额外的<option>
带有值的标签,timesheet_jobs
即外键字段。加载所有选项后,请帮我在最后添加一个额外的<option>
字段。<select>
表格.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)
解决方案
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')))
推荐阅读
- javascript - 在不重新加载的标签之间切换并限制打开具有相同 URL 的新标签
- entity-framework - 我可以将影子属性用于属性子项吗?
- python - 使用 Python 在 Mac 上打开应用程序
- java - 垂直合并单元格并将数据插入单元格
- python - 仅当行不以字符开头时,正则表达式才匹配模式
- node.js - 需要帮助使用 NodeJS 从 Azure Blobs 下载图像
- pom.xml - Maven 和 pom.xml 文件中的插件
- firebase - 匹配语句中的变量无法正常工作
- ajax - 如何在 Fancybox 弹出窗口中显示 DNN 模块?
- angular - 如何修复 Angular 项目中的刷新问题