Model:
class Subject(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
description = models.TextField(blank=True,null=True)
Forms:
class SearchTypeForm(forms.Form):
subject = forms.ModelChoiceField(queryset = Subject.objects.all())
Views:
...
context['search_type_form'] = SearchTypeForm()
...
Template:
{{search_type_form.subject}}
In actual templates the search type form is rendered as:
<select id="id_subject" name="subject">
<option selected="selected" value="">---------</option>
<option value="1">subject 1</option>
<option value="2">subject 2</option>
...
</select>
In the value field(of above dropdown list) is the id of the corresponding subject, How it be changed to some other field, like is we want to make it the slug field of Subject model.
You can override it by specifiing “to_field_name” keyword argument like this: