TAGS :Viewed: 12 - Published at: a few seconds ago

[ Django: CharField remove default ------— choice ]

In my models.py I have the following 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'),

How do I remove the default --------- choice from CharField? I tried inserting empty_label=None but not working.

And I cannot remove blank=True because it will prevent FileField() failed to capture the uploaded file.

My forms.py looks like this:

class Method1ClusteringForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(Method1ClusteringForm, self).__init__(*args, **kwargs)

Answer 1

I have found this which may be the solution for you, too.


from django.forms import ModelForm
from django import forms as forms

class Method1ClusteringForm(ModelForm):
    clustering_method_param = forms.forms.TypedChoiceField( 
                    initial = 'ward',
                    choices = (
                        ('complete', 'Complete linkage'),
                        ('average','Average linkage'),
    class Meta:
        model = Method1
        fields = ('inputfile_param', 'clustering_method_param',)

Answer 2

you must remove blank=True in your field definition. see here