How do I get rid of the Null choice in a select dropdown? For example, I have the following model --
class Network(models.Model):
type = models.CharField(max_length=10, choices = [('closed','closed'),('open','open')])
When I use the form in the template, I get three choices in the dropdown:
-------
Closed
Open
How do I get rid of this Null choice? So the select dropdown will only have the Closed and Open options?
From the documentation (via):
By default the widget used by ModelChoiceField will have an
empty choice at the top of the list. You can change the text of this
label (which is "---------" by default) with the empty_label
attribute, or you can disable the empty label entirely by setting
empty_label to None
Set blank=False in your model.
From the documentation:
Unless blank=False is set on the field along with a default then a label containing "---------" will be rendered with the select box. To override this behavior, add a tuple to choices containing None; e.g. (None, 'Your String For Display'). Alternatively, you can use an empty string instead of None where this makes sense - such as on a CharField.
choices = [("","-- Select an option --"),('closed','closed'),('open','open')]
Related
I've built an inline form, where the field labels don't show up, so the form relies on placeholders to tell the user what the field is:
The second field in is a ModelChoiceField. Is there any way to change the default "none" value to use the field name instead of the "---------" dashes?
forms.py
class ModelNameForm(forms.ModelForm):
field = forms.ModelChoiceField(queryset=ModelName.objects.all(),empty_label="*** SomeThing **", label="Department")
I have a model with many boolean flags. Some of them are displayed in admin change list and made editable by list_editable - this makes them appear as checkboxes. The problem is that this wastes a lot of horizontal space as field names are long (and I want to keep them descriptive for model change form). For regular fields I use custom properties and short_description to shorten the name. But in that case I have to render checkbox but I do not know how to make it properly.
Or may be there is another hack to alter field name for change list only?
If I got you right, the verbose_name model attribute is what you're looking for.
new_tab = models.BooleanField(verbose_name='n_tab')
This way new_tab field will appear as n_tab in admin interface. You can set verbose_name to empty string too.
I'm trying to create a form consisting of a multiple select field which is used to select multiple instances of my Person model.
class MyForm(forms.Form):
choices = [(p.id, str(p)) for p in Person.objects.all()]
my_field = forms.ChoiceField(widget=forms.SelectMultiple, choices=choices)
The widget looks exactly like I want, but when I submit the form, it fails with the message
Select a valid choice. ['2', '3'] is not one of the available choices.
What am I doing wrong? When removing the widget=forms.SelectMultiple, from the third line, it works, but then it's only a single select field.
You are getting the error because ChoiceField expects a single choice.
If you want to allow multiple choices, use a MultipleChoiceField.
my_field = forms.MultipleChoiceField(choices=choices)
Note you don't have to specify the widget, as it's forms.SelectMultiple by default.
Looking for a way to use radio buttons in the Django admin. I would like to have 3 or 4 hard coded options the user can select from and I will use that selection to know what to do with the next text field. The options never need to change. Can anyone point me in the right direction? Google is not cooperating on this one...
I'm going to assume you want this on the change page for a Foo model object. Let's call the extra field my_option.
First, you'll need to use a custom form in your FooAdmin class so you can get the extra field in there:
class FooAdmin:
...
form FooForm
fields = (..., 'my_option', ...)
...
Then, just create the form with the extra option:
class FooForm(forms.ModelForm):
class Meta:
model = Foo
my_option = ChoiceField(
label = 'Options',
choices = (
(0, 'Don\'t change anything.'),
(1, 'Do some crazy stuff.'),
(2, 'Do other stuff.'),
),
initial = 0,
widget = RadioSelect,
)
ChoiceField defaults to using a select box, which is why you also need to override the widget type in the field's constructor. You can also check out TypedChoiceField if that serves your needs better.
Finally, you'll need to handle the field somewhere. I suggest overriding the clean() method in FooForm to get the value and set values in self.cleaned_data depending on what you want the option to do.
i have a form with a ChoiceField and a CharField. Now i want to enable the CharField just when i select a specific choice of the ChoiceField (e.g. choice 3). Otherwise the CharField should be disabled.
My ChoiceField
choices = (('1', 'some text',),
('2', 'some text',),
('3', 'some text',))
host = forms.ChoiceField(choices = choices)
And a simple CharField
hostAdress = forms.CharField()
Do you have any ideas to get it this way?
you can start with the char field disabled/hidden and use javascript to catch change event on the select box and change the charfield on that event. For example:
$("#id_choice").change(function(eObj) {
$("#id_char").removeAttr("disabled");
this is just an example (using jQuery), you need to make sure to disable the charfield again if the select box is unselected (not sure if you allow this behaviour) and you also need to handle the display of form errors properly if you hide the field and not enable/disable it.
I would use jQuery for this.
$('your-select-input-selector').change(function() {
var bDisable = true;
if ($(this).val() == '3'):
bDisable = !bDisabled;
$('your-text-input-selector').prop('disabled', bDisable);
});
You also need to handle the case where the select input is already set on the value 3 when you first load the page.