9π
Even without blank=True it shows the extra input. I have created a new Widget:
from itertools import chain
from django.forms import RadioSelect
from django.utils.encoding import force_unicode
class RadioSelectNotNull(RadioSelect):
def get_renderer(self, name, value, attrs=None, choices=()):
"""Returns an instance of the renderer."""
if value is None: value = ''
str_value = force_unicode(value) # Normalize to string.
final_attrs = self.build_attrs(attrs)
choices = list(chain(self.choices, choices))
if choices[0][0] == '':
choices.pop(0)
return self.renderer(name, str_value, final_attrs, choices)
- Celery and signals
- How to match redis key patterns using native django cache?
- Coerce in django forms
- Cleanest way to delete stale ContentTypes?
- Django 1.4 Unknown command: 'runserver'
1π
You can set the choices when you set the widget. Itβs showing the β- because in your model you have blank=True.
Just use the choices arg of the widget and set it to the choices you set in your model.
- Saving Base64ImageField Type using Django Rest saves it as Raw image. How do I convert it to a normal image
- Making transient (non-database) attributes in Django model available to template
- Django View Causes Psycopg2 Cursor Does/Does Not Exist Error
- Python ctypes MemoryError in fcgi process from PIL library
1π
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:
A custom empty label:
field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")
No empty label:
field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
0π
Django <= 1.10
RadioSelectNotNull widget
from itertools import chain
from django.forms import RadioSelect
from django.utils.encoding import force_unicode
class RadioSelectNotNull(RadioSelect):
"""
A widget which removes the default '-----' option from RadioSelect
"""
def get_renderer(self, name, value, attrs=None, choices=()):
"""Returns an instance of the renderer."""
if value is None: value = ''
str_value = force_unicode(value) # Normalize to string.
final_attrs = self.build_attrs(attrs)
choices = list(chain(self.choices, choices))
if choices[0][0] == '':
choices.pop(0)
return self.renderer(name, str_value, final_attrs, choices)
Django >= 1.11
As after Django 1.10 following method is no more in RadioSelect or in its ancestors. Thatβs why upper widget will not remove bogus choice generated by RadioSelect.
def get_renderer(self, name, value, attrs=None, choices=()):
So to remove bogus choice generated by RadioSelect use following widget. I have tested this till Django 2.0
from django.forms import RadioSelect
class RadioSelectNotNull(RadioSelect):
"""
A widget which removes the default '-----' option from RadioSelect
"""
def optgroups(self, name, value, attrs=None):
"""Return a list of optgroups for this widget."""
if self.choices[0][0] == '':
self.choices.pop(0)
return super(RadioSelectNotNull, self).optgroups(name, value, attrs)
- Django "MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in)"
- Django REST Framework β POSTing foreign key field containing natural key?