Django表单和选择字段

时间:2015-08-23 20:52:33

标签: django django-models django-forms

我有以下型号:

class StraightredTeam(models.Model):
    teamid = models.IntegerField(primary_key=True)
    teamname = models.CharField(max_length=36)
    country = models.CharField(max_length=36,null=True)
    stadium = models.CharField(max_length=36,null=True)
    homepageurl = models.TextField(null=True)
    wikilink = models.TextField(null=True)
    teamcode = models.CharField(max_length=5,null=True)
    teamshortname = models.CharField(max_length=24,null=True)
    currentteam = models.PositiveSmallIntegerField(null=True)

    def natural_key(self):
        return self.teamname

    class Meta:
        managed = True
        db_table = 'straightred_team'

我使用以下形式的此模型:

from django import forms
from straightred.models import StraightredTeam

    class SelectTwoTeams(forms.Form):

        currentTeams = StraightredTeam.objects.filter(currentteam = 1).order_by('teamname')
        team_one = forms.ModelChoiceField(queryset = currentTeams, to_field_name = "teamname")

这会产生以下html:

            <form action="" method="post">
                <input type='hidden' name='csrfmiddlewaretoken' value='xyz' />
                <tr><th><label for="id_team_one">Team one:</label></th><td><select id="id_team_one" name="team_one">
<option value="" selected="selected">---------</option>
<option value="Arsenal">StraightredTeam object</option>
<option value="Aston Villa">StraightredTeam object</option>
<option value="Bournemouth">StraightredTeam object</option>
<option value="Chelsea">StraightredTeam object</option>
<option value="Crystal Palace">StraightredTeam object</option>
<option value="Everton">StraightredTeam object</option>
<option value="Leicester">StraightredTeam object</option>
<option value="Liverpool">StraightredTeam object</option>
<option value="Man City">StraightredTeam object</option>
<option value="Man United">StraightredTeam object</option>
<option value="Newcastle">StraightredTeam object</option>
<option value="Norwich">StraightredTeam object</option>
<option value="Southampton">StraightredTeam object</option>
<option value="Stoke">StraightredTeam object</option>
<option value="Sunderland">StraightredTeam object</option>
<option value="Swansea">StraightredTeam object</option>
<option value="Tottenham">StraightredTeam object</option>
<option value="Watford">StraightredTeam object</option>
<option value="West Brom">StraightredTeam object</option>
<option value="West Ham">StraightredTeam object</option>
</select></td></tr>
                <input type="submit" value="Submit" />
            </form>

我很想要&#34; StraightredTeam对象&#34;显示与值部分相同。即足球/足球队的名字。

任何指出我正确方向的建议都是理想的,非常感谢,Alan。

2 个答案:

答案 0 :(得分:4)

您需要在模型上定义__unicode__方法(或__str__,如果您使用的是Python 3)。

class StraightredTeam(models.Model):
    ...
    def __unicode__(self):   # __str__ on Python 3
        return self.teamname

但请注意,您不应在字段定义中使用该to_field_name属性。默认是使用ID,这是允许Django正确设置外键的正确值。

答案 1 :(得分:2)

通过将对象转换为unicode(python 2.x)或string(python 3.x),可以在django表单中创建选择标签。如果您想在此处显示teamname,只需创建__unicode__(python 2.x,您也可以创建__str__方法并使用从{{1}导入的python_2_unicode_compatible修饰对象所以它将兼容python 2.x和3.x)或django.utils.encoding(python 3.x)方法,它将返回字符串以显示为选择标签。