2012-11-19 65 views
1

我在尝试重写Django ModelForm字段时遇到了问题。重写Django ModelForm字段时出错

我的models.py是这样的:

from django.db import models 

class CadastroParticipantes(models.Model): 
    nome = models.CharField(max_length=50) 
    sobrenome = models.CharField(max_length=100) 
    cpf = models.CharField(max_length=14) 
    email = models.EmailField(max_length=100) 
    num_cartas_solicitadas = models.IntegerField(default=0) 

    def __unicode__(self): 
     return self.email 

而且我forms.py是这样的:

from django.forms import ModelForm 
from models import * 

class FormCadastroParticipante(ModelForm): 
    class Meta: 
     model = CadastroParticipantes 
     fields = ('nome', 'sobrenome', 'cpf', 'email') 
     exclude=('num_cartas_solicitadas') 
     widgets = { 'nome' : attrs={'title': 'Seu primeiro nome.'}} 

当我运行的服务器,并尝试访问我得到这个消息:

**

SyntaxError at/
invalid syntax (forms.py, line 9) 

**

有人可以帮助我吗?

在此先感谢所有! (Y)

回答

2

你已经忘了指定窗口小部件类。它应该是这样的:

from django.forms.widgets import TextInput 

class FormCadastroParticipante(ModelForm): 
    class Meta: 
     model = CadastroParticipantes 
     fields = ('nome', 'sobrenome', 'cpf', 'email') 
     exclude=('num_cartas_solicitadas',) 
     widgets = { 'nome' : TextInput(attrs={'title': 'Seu primeiro nome.'}), } 

更改TextInput到您想要的widget类。请注意0​​类中的exclude属性接受一个列表,如果只有一个列表成员,请不要忘记尾随逗号。

+0

谢谢你!它非常完美! :) –

0

你为什么不定义attrs={'title': 'Seu primeiro nome.'}课外元,然后你可以说widgets = { 'nome' : attrs}