Django MultipleCheckBox没有显示正确的值并显示错误

时间:2015-10-25 17:33:18

标签: django django-models django-forms django-templates

我有一个问题,当我调用我的表单时,基于外键的select字段没有显示我的DB包含的值,它显示了我的ModelClass对象。

Models.py

class AddTurma(models.Model):

    TURMA = (
        ('0', 'Selecione a Turma'),
        ('1', '1º'),
        ('2', '2º'),
        ('3', '3º'),
        ('4', '4º'),
        ('5', '5º'),
        ('6', '6º'),
        ('7', '7º'),
        ('8', '8º'),
        ('9', '9º'),
        )

    TURNO = (
        ('1', 'Selecione o Turno'),
        ('M', 'Manhã'),
        ('T', 'Tarde'),
        ('N', 'Noite'),
        )

    ETAPA = (
        ('1', 'Selecione a etapa de ensino'),
        ('F', 'Fundamental'),
        ('M', 'Médio'),
        ('C', 'Cursinho'),
        )

    SALA = (
        ('1', 'Selecione a sala'),
        ('A', 'A'),
        ('B', 'B'),
        ('c', 'C'),
        ('D', 'D'),
        ('E', 'E'),
        )

    turma = models.CharField(max_length = 1,  null=True, choices=TURMA, default = '0')
    sala = models.CharField(max_length = 1, null=True, choices=SALA, default = '1')
    etapa = models.CharField(max_length =1, choices = ETAPA, default = '1')
    turno = models.CharField(max_length = 1, choices = TURNO, default = '1')

class AddProfessor(models.Model):
    nome = models.CharField(max_length=20)
    sobrenome = models.CharField(max_length=100)
    nascimento = models.DateField()
    matricula = models.PositiveIntegerField()
    disciplina = models.CharField(null=True, max_length=20)
    email = models.EmailField(null=True, )
    senha = models.CharField(max_length=20)
    turmas = models.ForeignKey(AddTurma,default='1')

forms.py

class FormAddProfessor(forms.ModelForm):
    class Meta:
        model = AddProfessor
        fields = '__all__' #'nome', 'sobrenome', 'nascimento', 'matricula', 'disciplina', 'senha'
        widgets = {
            'turmas': forms.CheckboxSelectMultiple(),
        }

class FormAddTurma(forms.ModelForm):
    class Meta:
        model = AddTurma
        fields = '__all__'

模板addprofessor

{% extends 'base.html' %}


{% block corpo %}

<form action="" method="POST">

    {% csrf_token %}
    {{ form.as_p }}

    <button type="submit">Adicionar</button>
</form>
<br />
<br />
<p><a href="/menu_direcao/">Voltar para o menu DIREÇÃO.</a></p>

{% endblock %}

我的数据库中有4个值,当我在addprofessor模板上时,它会显示所有表单,但多个复选框显示:

AddTurma object
AddTurma object
AddTurma object
AddTurma object

当我选择任何选项时,我收到此消息:

Select a valid choice. That choice is not one of the available choices.
你能帮帮我吗? 感谢

0 个答案:

没有答案