23👍
✅
Use the default
attribute:
display = models.CharField(..., default=FRIENDS)
or
display = models.CharField(..., default=CHOICES[1][1])
3👍
You could just set the default attribute:
display = models.CharField(default='F', max_length=1, choices=CHOICES, blank=True, null=True)
- Django-autocomplete-light simple usage
- Why does docker-compose build not reflect my django code changes?
- Invalid data. Expected a dictionary, but got str error with serializer field in Django Rest Framework
- Django: How do I use a string as the keyword in a Q() statement?
0👍
There is a ChoiceField attribute for RadioButtons, then the solution could be with "initial" attribute… Example:
from django import forms
CHOICES = [("1", "First choice"), ("2", "Second choice")]
class ContactForm(forms.Form):
choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES, initial="1")
0👍
Note that this is an old question. As of Django 3.0 the canonical way is to use the models.TextChoices
class.
The following is an example that uses the Enum
functional api:
CHOICES = models.TextChoices(
"Display", (
("O", "Me"),
("F", "Friends"),
("P", "Public"),
)
)
You can then write
display = models.CharField(
max_length=1, choices=CHOICES, default=CHOICES.F, blank=True, null=True
)
- Where has 'django.core.context_processors.request' gone in Django 1.10?
- MultiValueDictKeyError generated in Django after POST request on login page
- Csrf_token of Django into Vuejs when seperate them
Source:stackexchange.com