17👍
✅
This is kind of a hacky solution, but I’ve tested it to work: simply set the default for the field to a value that isn’t one of the choices (I recommend setting it to None
). When rendering the form, Django won’t know which input to mark as checked, so it will leave all of them unchecked (without throwing an error). And the fact that there is a default means there will be no auto-generated input field.
12👍
In Django 1.6 (didn’t test it in other versions) all you need is default=None
:
class myModel(models.Model):
choose = models.CharField(..., default=None)
class myModelForm(ModelForm):
class Meta:
model = myModel
widgets = {
'choose': RadioSelect(),
}
- Django forms give: Select a valid choice. That choice is not one of the available choices
- Multiple lookup_fields for django rest framework
- Celery beat not picking up periodic tasks
4👍
The previous solution wouldn’t work for me.
Therefore I just slice out the first widget’s choice.
class myModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.fields['choose'].choices = self.fields['choose'].choices[1:]
class Meta:
model = myModel
widgets = {
'choose': RadioSelect(),
}
- What is the right way to use angular2 http requests with Django CSRF protection?
- Django logging – django.request logger and extra context
- What does it mean for an object to be unscriptable?
Source:stackexchange.com