2012-06-25 40 views
1

當在Form.py中使用ModelForm時,它將節省大量時間,因爲不需要在窗體中重新重寫整個字段。ModelForm和現場生成

class ContactsForm(ModelForm): 
    class Meta: 
     model = Contact 

以forms.Form取而代之將需要手動再次定義所有字段。由於更多的控制,有些人會喜歡這種方法

雖然許多是直截了當:

models.CharField("First Name",max_length=30, blank=True) 

成爲

forms.CharField(label = _(u'First Name'), max_length=30, blank=True) 

models.TextField(blank=True)成爲forms.TextArea(blank=True)等等

的字段是一個有點神祕的對我它將如何翻譯成各種形式,如:

models.ForeignKey(ContactType) 

如何在窗體中定義dropdownmenu?

回答

1

你的意思是這樣的?:

contact_type = forms.ModelChoiceField(queryset=ContactType.objects.all()) 

更多ModelChoiceField上Django的documentation

1

您正在尋找forms.ModelChoiceField