Reputation:
I have a modelform with an element pointing to a foreignkey meaning its by default a modelmultiplechoice field but i have changed this in the meta class so its using a select field instead since i wanted a dropdown list that contains values pointed to by the foreign key (the foreign key points to a table with categories).
This all works fine but on the form the default element displayed is "----" and ive read at this page: https://docs.djangoproject.com/en/1.6/ref/forms/fields/#modelchoicefield that i should be able to use the empty_label to change this so a most descriptive/more user friendly value. I wanted to change it to a value that actually exists but the empty_label doesnt seem to support this and when using a foreign key with a modelform i havent seen functionality elsewhere that lets me do this.
I tried implementing this by adding an option in the modelform' meta class like this:
'categoryID': Select(attrs={'class': 'form-control', 'empty_label': 'hello'})
I didnt get any syntax errors but the default value of the select box did not change, any ideas on why this isnt working ?
Upvotes: 0
Views: 61
Reputation: 599540
If you want to use a value from the field's choices, you shouldn't be using "empty_label" - apart from anything else, that really is for an empty value, so the value sent to the server when it is selected will just be "".
Instead, you should provide the initial
parameter, either when defining the field, or when you instantiate the form.
Upvotes: 1