2011-07-18 96 views
1

是否可以在Model上寫入字段大小?是否可以在Model上設置字段大小?

這是我form.py,我願做這樣的事情在我的模型部分: 電子郵件= forms.EmailField(標籤= 「電子郵件」,窗口小部件= forms.TextInput(ATTRS = {「大小「:」 60'

我不能上因爲我使用類關係定義這個屬性。如果我宣佈我的父類的‘電子郵件’和我的孩子上課贏得利用這個領域,它。將顯示它(模型排除無法工作,因爲「父母」上聲明瞭「email」屬性)

models.py

class UserProfile(models.Model): 
    nome = models.CharField('Nome Completo',max_length=100) 
    endereco = models.CharField('Endereço',max_length=100) 
    ... 

forms.py

class **cadastrousuarioForm**(formulariocadastrocompleto): 
    nome = forms.CharField(label = 'Nome',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'})) 
    ... 

class atualizacaoousuarioForm(**cadastrousuarioForm**): 

    class Meta (cadastrousuarioForm): 
     model = UserProfile 
     fields = ("endereco",) 
     exclude = ("nome") 
     ... 

回答

2

沒有,場大小不能在模型中定義。當你在父項上定義字段時,你所描述的問題排除不在子項上工作,這實際上是Django中的一個開放性錯誤。從某種意義上說,你的挫折是有保證的,但不幸的是,你沒有太多的選擇。

你可能想嘗試一個mix-in:

class MyFieldMixIn(forms.ModelForm): 
    my_field = forms.CharField(label = 'Nome',widget=forms.TextInput(attrs={'size':'30','maxlength':'100'})) 

class ParentForm(forms.ModelForm): 
    # Common functionality, fields, etc. here 

class ReplacementParentForm(ParentForm, MyFieldMixIn): 
    pass 

class ChildForm(ParentForm): 
    # Child-specific stuff here 

然後,而不是使用您使用替換父窗體,所以你只能在該表單字段。

相關問題