Django ChoiceField Empty String Value

Hi, wondering if we can set empty string as value for select option?

Django version: 4.0.2

Example

CHOICE_LIST = [
    ("", "----"),
    (1, "Rock"),
    (2, "Hard Place"),
]

choices = forms.ChoiceField(
    choices=CHOICE_LIST,
    required=False,
)

The html generated is same for ("", "----") and (None, "----")

<select class="form-select form-select-sm mb-2" id="id_choices" name="choices">
  <option selected="">----</option>
  <option value="1">Rock</option>
  <option value="2">Hard Place</option>
</select>

If we use something else instead of "" or None for value, it will be what I want:

<select class="form-select form-select-sm mb-2" id="id_choices" name="choices">
  <option value="Somethingelse">----</option>
  <option value="1">Rock</option>
  <option value="2">Hard Place</option>
</select>

Reason I am using "" is because this field is TextField

Thanks

I can’t recreate the behavior you’re describing here. My rendering of that field generates:
<option value="" selected>-----</option>

How are you rendering that field and those options? Are you overriding any of the system widgets? Or are you using a custom widget?

I am using django widget tweaks, not using custom widget.

{{ choices|add_class:"form-select form-select-sm mb-2" }}

I will try bootstrap a new instance and try this to confirm. Thanks for the pointer.

I confirm you are right, this is not django issue, but other libraries’ fault, I am closing this

This is what I see in new setup.

<select name="choices" id="id_choices">
  <option value="" selected="">----</option>
  <option value="1">Rock</option>
  <option value="2">Hard Place</option>
</select>