I have a many to many relationship model which actually shows as a multi select list on forms. In one particular place I want to show it as dropdown single selection - any idea how to do this?
A:
See the documentation on overriding default field types or widgets.
If you've got a Book
model, with a ManyToMany
relationship to Author
, like this:
class Author(models.Model):
name = models.CharField(max_length=100)
title = models.CharField(max_length=3, choices=TITLE_CHOICES)
def __unicode__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
then you can do something like this:
from django.forms import ModelForm, Select
class AuthorForm(ModelForm):
class Meta:
model = Author
widgets = {
'name': Select(),
}
NB. This code is not tested, but will hopefully be enough to get you on your way.
Dominic Rodger
2010-08-17 09:53:18