2010-08-17 70 views

回答

2

請參閱關於overriding default field types or widgets的文檔。

如果你已經有了一個Book模型,用ManyToMany關係Author,像這樣:

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) 

那麼你可以做這樣的事情:

from django.forms import ModelForm, Select 

class AuthorForm(ModelForm): 
    class Meta: 
     model = Author 
     widgets = { 
      'name': Select(), 
     } 

NB。這段代碼沒有經過測試,但希望能夠讓你順利。