Dropdown in Django Model
I want to create a field in Django models.py
which will render as a dropdown and user can select the options from there.
If I have 5 choices:
- GREEN
- BLUE
- RED
- ORANGE
- BLACK
How should I write my code in models.py
and Forms.py
so that the template renders it like a dropdown element?
From model to template :
models.py
COLOR_CHOICES = (
('green','GREEN'),
('blue', 'BLUE'),
('red','RED'),
('orange','ORANGE'),
('black','BLACK'),
)
class MyModel(models.Model):
color = models.CharField(max_length=6, choices=COLOR_CHOICES, default='green')
forms.py
class MyModelForm(ModelForm):
class Meta:
model = MyModel
fields = ['color']
views.py
class CreateMyModelView(CreateView):
model = MyModel
form_class = MyModelForm
template_name = 'myapp/template.html'
success_url = 'myapp/success.html'
template.html
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create" />
</form>
or to display your select field only :
{{ form.color }}
Specify CharField or IntegerField with choices
option in your model https://docs.djangoproject.com/en/dev/ref/models/fields/#choices and use ModelForm https://docs.djangoproject.com/en/dev/topics/forms/modelforms/.
An even better option in Django version 3.0 and higher:
You can use inner classes which inherit from the new models.TextChoices class. That also provides a convenient way to provide human readable (and translatable) labels, as well as a nice interface for the programmer.
Official Django documentation
from django.utils.translation import gettext_lazy as _
class Student(models.Model):
class YearInSchool(models.TextChoices):
FRESHMAN = 'FR', _('Freshman')
SOPHOMORE = 'SO', _('Sophomore')
JUNIOR = 'JR', _('Junior')
SENIOR = 'SR', _('Senior')
GRADUATE = 'GR', _('Graduate')
year_in_school = models.CharField(
max_length=2,
choices=YearInSchool.choices,
default=YearInSchool.FRESHMAN,
)
def is_upperclass(self):
return self.year_in_school in {
self.YearInSchool.JUNIOR,
self.YearInSchool.SENIOR,
}
###########
# human readable text can then be called by...
Student.YearInSchool.Junior.label
# or from an instance by <instance>.get_<innerclass name>_display() like...
student_inst.get_yearinschool_display()