3

Here in Forms we can use the empty_label. I use widget Select, my choices are from database, does it possible to use here empty_label? In model I have:

position = forms.IntegerField(label=position_label, required=False, 
    widget=forms.Select(choices=Position.objects.all().values_list('id', 'position'),
    attrs={'class':'main', 'title': position_label},
    ), empty_label="(Empty)")

But the error is:

TypeError: __init__() got an unexpected keyword argument 'empty_label'

How to set the label to 'Empty'?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Leon
  • 6,316
  • 19
  • 62
  • 97

3 Answers3

10

There are two ways to implement empty_label:

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['field_name'].empty_label = "(Select here)"
        self.fields['field_name'].widget.attrs['class'] = 'main'
        self.fields['field_name'].queryset = Position.objects.all().values_list('id', 'position')

//OR   

class MyForm(forms.ModelForm):
    field_name = forms.ModelChoiceField(
        queryset=Position.objects.all().values_list('id', 'position'), 
        empty_label="(Select here)"
        )

    class Meta:
        model = MyModel
catherine
  • 22,492
  • 12
  • 61
  • 85
  • 1
    Thanks for .widget.attrs['class']. I thought the shortest method was: .widget = forms.TextInput(attrs={'class': '...'}) – Adam Nov 26 '13 at 21:04
3

empty_label is a property of the field, not the widget. You already have a label set for position.

Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144
  • But I need empty_label 'Empty', not first 'position' from db – Leon Mar 05 '13 at 12:22
  • Then you need to revise your question. Your question was how can I fix the TypeError, not how to set the label to 'Empty' – Brandon Taylor Mar 05 '13 at 12:26
  • Oh, yeah. Sorry for my inattantion. And does it possible [to set the label to 'Empty']? How do this? – Leon Mar 05 '13 at 12:31
  • Since you're returning a values_list, you can simply insert the value you need at the beginning of the list. Assuming `foo` is a list: foo.insert(0, ('Empty', '',)). More information on lists: http://docs.python.org/2/tutorial/datastructures.html – Brandon Taylor Mar 05 '13 at 12:34
  • `AttributeError: 'ValuesListQuerySet' object has no attribute 'insert'` I use `foo = Position.objects.all().values_list('id', 'position')` – Leon Mar 05 '13 at 12:48
  • 1
    Convert the result of the ORM call to a list first, then you can use `insert` – Brandon Taylor Mar 05 '13 at 12:50
0

from the Django Documentation https://docs.djangoproject.com/en/1.10/ref/forms/widgets/#django.forms.SelectDateWidget

If the DateField is not required, SelectDateWidget will have an empty choice at the top of the list (which is --- by default). You can change the text of this label with the empty_label attribute. empty_label can be a string, list, or tuple. When a string is used, all select boxes will each have an empty choice with this label. If empty_label is a list or tuple of 3 string elements, the select boxes will have their own custom label. The labels should be in this order ('year_label', 'month_label', 'day_label').

Sevalad
  • 69
  • 6