ModelMultipleChoiceField返回" name Object"

时间:2014-06-25 14:23:12

标签: html django

使用ModelMultipleChoiceField时遇到问题。我有一个名为Instrumentation的模型和其他名为InstTemplate:

class Instrumentation(models.Model):
    code        = models.CharField(max_length=70, unique=True)
    marca       = models.CharField(max_length=70)
    modelo      = models.CharField(max_length=70)
    familia     = models.CharField(max_length=150)
    subfamilia  = models.CharField(max_length=150)
    calibration_date    = models.DateField()
    #test       = models.ForeignKey(Test)
    notes       = models.TextField(max_length=170, blank=True)
    utilization = models.DateField(blank=True)


    def is_free(self):
        return (self.utilization == None)

    def is_calibrated(self):
        return (self.calibration_date > date.today())



class InstTemplate(models.Model):

    name        = models.CharField(max_length=70)
    equipo      = models.ManyToManyField(Instrumentation)
    boards      = models.CharField(max_length=20)
    project     = models.ForeignKey(Project)
    notes       = models.TextField(max_length=170, blank=True)

我想在“equipo”字段中实现带有复选框的多选,这是一个ManyToManyField。正如我所读到的,这种多重选择在forms.py中实现:

class InstTemplateForm(forms.ModelForm):

        BOARD_CHOICES = (('1','ML801/AP815'),('2','ML455/AP455'),('3','ML801/AP801'),('4','ML801/AP836i'), ('5','ML801/AP809'), ('6','CANHEAD'), ('7','ML74/AP74'),('8','ML74/AP75'))
        boards = forms.MultipleChoiceField(BOARD_CHOICES, widget=forms.CheckboxSelectMultiple())

        equipo = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=Instrumentation.objects.all())

        class Meta:
            model = InstTemplate

但是在HTML中,使用以下代码,我只获得了数据库“id”字段和名称“Instrumentation object”,用于查询集获取的所有对象。因此,查询集正在工作,但我只获得“Instrumentation对象”。

{% block page %}
<div id="form" class="tab-content clearfix" style="display: block;">
    <h4><strong>New instrumentation template: project {{ project.ref }}</strong></h4>
    <hr class="alt1"/>
    <form class="vertical" action="{% url "new_instrumentation" project.ref %}" method="post"> {% csrf_token %}

    {{ form.as_ul }}

    </form>
    </div>

{% endblock %}

enter image description here

如何获取Instrumentation对象的字段?

1 个答案:

答案 0 :(得分:2)

Instrumentation模型中定义__unicode__方法,以获取模型对象的人类可读表示形式:

class Instrumentation(models.Model):
    code        = models.CharField(max_length=70, unique=True)
    marca       = models.CharField(max_length=70)
    modelo      = models.CharField(max_length=70)
    familia     = models.CharField(max_length=150)
    subfamilia  = models.CharField(max_length=150)
    calibration_date    = models.DateField()
    #test       = models.ForeignKey(Test)
    notes       = models.TextField(max_length=170, blank=True)
    utilization = models.DateField(blank=True)


    def is_free(self):
        return (self.utilization == None)

    def is_calibrated(self):
        return (self.calibration_date > date.today())

    def __unicode__(self):
        return self.code # or self.marca what ever you want