我已经在我的管理表单中添加了一个自定义小部件,只显示数据,当我尝试保存表单时,我的自定义小部件需要获取字段,如何将其设置为不需要?
由于
forms.py
class TemplateVariablesWidget(forms.Widget):
template_name = 'networks/config_variables.html'
def render(self, name, value, attrs=None):
c_vars = ConfigVariables.objects.all()
context = {
'ConfigVariables' : c_vars
}
return mark_safe(render_to_string(self.template_name, context))
class VariableForm(forms.ModelForm):
variables = forms.CharField(widget=TemplateVariablesWidget)
class Meta:
model = ConfigVariables
fields = "__all__"
admin.py
class ConfigTemplateAdmin(admin.ModelAdmin):
form = VariableForm
list_display = ('device_name', 'date_modified')
admin.site.register(ConfigTemplates, ConfigTemplateAdmin)
答案 0 :(得分:-1)
在Form fields部分查看Django文档。
class VariableForm(forms.ModelForm):
variables = forms.CharField(widget=TemplateVariablesWidget, required=False)
class Meta:
model = ConfigVariables
fields = "__all__"
但是,请注意,检查模型中是否需要此字段。