Note that there are some explanatory texts on larger screens.

plurals
  1. POModelChoiceField , removing the blank choice
    primarykey
    data
    text
    <p>I want to get rid of the "-------------" choice Django adds in a select input representing a Foreign Key on a ModelForm</p> <p>It's <a href="https://stackoverflow.com/questions/1868211/django-modelchoicefield-remove-default-choice">been answered</a> that you can use the <code>empty_label=none</code> option, but I have a ModelForm, not a regular form and overriding the field is not allowed. </p> <p>I know that I can override the <code>__init__()</code> method of the ModelForm in order to modify a ModelChoiceField's queryset using </p> <pre><code>self.fields['my_foreign_key'].queryset = .... </code></pre> <p>But this would be really ugly, as this happens over +10 foreign_keys on the "Main" model, and there's more than a Modelform based on this model</p> <p>The whole context : </p> <ul> <li>each one of these foreign_key points to the same kind of models : they are particular lists of choices, many models to ease their modification via the admin.</li> <li>all these models are related to the Main model via a "to_field=code" relation, based on a Charfield which contains a three-letter code (hey, not my fault, I had to use a legacy MS Access DB), this CharField has the <code>blank=True, unique=True</code> options, so I could, for each list, create a <code>"---No information yet---"</code> record which has, you bet, a blank value instead of a three letter code...</li> </ul> <p>The situation is now : I get a select input field with two "blank value" choices : the django one and mine, one after the other. I just miss a 'empty_label=none` option here too...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload