问题描述
我在django中有一个文本框和文件格式的表单。它应该让使用将文本粘贴到该框或上传文件。如果用户将文本粘贴到框中,我不需要检查fileField。如何使form.FileField()可选?
p>如果您在 forms.Form
派生类中使用 forms.FileField()
,则可以设置:
class form(forms.Form):
file = forms.FileField(required = False)
如果您使用的是 models.FileField()
并且分配给该模型的 forms.ModelForm
可以使用
class amodel(models.Model):
file = models.FileField(blank = True,null = True)
您使用的是取决于您如何导出表单,以及是否使用基础ORM(即模型)。
I have form with a textbox and filefield in django. It should let the use either paste the text into that box or upload a file. If the user has pasted the text into the box, I needn't check the fileField.
How do I make the forms.FileField() optional?
If you're using a forms.FileField()
in a forms.Form
derived class, you can set:
class form(forms.Form):
file = forms.FileField(required=False)
If you're using a models.FileField()
and have a forms.ModelForm
assigned to that model, you can use
class amodel(models.Model):
file = models.FileField(blank=True, null=True)
Which you use depends on how you are deriving the form and if you are using the underlying ORM (i.e. a model).
这篇关于如何使FileField在django可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!