[英]Django: CharField remove default ------— choice
In my models.py I have the following CharField在我的models.py我有以下 CharField
class Method1(models.Model):
inputfile_param = models.FileField()
clustering_method_param = models.CharField(max_length=20,
default='ward', blank=True, choices=(
('complete', 'Complete linkage'),
('average','Average linkage'),
('ward','Ward'),))
How do I remove the default --------- choice from CharField?如何从 CharField 中删除默认的 --------- 选择? I tried inserting empty_label=None
but not working.我尝试插入empty_label=None
但不起作用。
And I cannot remove blank=True
because it will prevent FileField()
failed to capture the uploaded file.而且我无法删除blank=True
因为它会阻止FileField()
无法捕获上传的文件。
My forms.py looks like this:我的forms.py看起来像这样:
class Method1ClusteringForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Method1ClusteringForm, self).__init__(*args, **kwargs)
I have found this which may be the solution for you, too.我发现这也可能是您的解决方案。
Try:尝试:
from django.forms import ModelForm
from django import forms as forms
class Method1ClusteringForm(ModelForm):
clustering_method_param = forms.forms.TypedChoiceField(
required=True,
initial = 'ward',
choices = (
('complete', 'Complete linkage'),
('average','Average linkage'),
('ward','Ward'),)
)
class Meta:
model = Method1
fields = ('inputfile_param', 'clustering_method_param',)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.