0
我有以下几点看法Django的更新表单字段返回空白
class ProductUpdateView(BaseProductUpdateView):
form_class = ProductForm
slug_field = "id"
def form_valid(self, form):
form.instance.hotel = form.cleaned_data["hotel"]
form.instance.parent = form.cleaned_data["parent"]
return super(ProductUpdateView, self).form_valid(form)
和形式如下:
class ProductForm(BaseProductForm):
hotel = forms.ModelChoiceField(queryset=Hotel.objects.all(), widget=forms.TextInput)
parent = forms.ModelChoiceField(queryset=Product.objects.all(), widget=forms.TextInput, required=False)
class Meta:
model = Product
exclude = ('slug', 'parent', 'hotel', 'status', 'score', 'product_class',
'recommended_products', 'related_products',
'product_options', 'attributes', 'categories')
当我保存表单,表单保存成功。我可以在管理员中看到酒店和家长的保存值,但是当我重新打开更新表单页面时,其他字段会返回,但酒店和父级字段会返回空白。有任何想法吗?
ModelChoiceField并不意味着与一个TextInput使用。相反,为什么不使用[意味着与选项配合工作的小部件](https://docs.djangoproject.com/en/dev/ref/forms/widgets/#widgets-inheriting-from-the-select-widget) ? – jpic
我需要一个输入文本类型字段。我使用select2进行自动完成。但即使我禁用select2。字段为空 – tunaktunak
为什么你排除这两个字段? –