Alan Tingey
Alan Tingey

Reputation: 971

Django forms and the select field

I have the following model:

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'

I use this model with the following form:

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")

This produces the following 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>

I would love for the "StraightredTeam object" to show the same as the value part. I.e. the name of the football/soccer team.

Any advice to point me in the right direction would be ideal, many thanks, Alan.

Upvotes: 1

Views: 2144

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 600059

You need to define a __unicode__ method (or __str__ if you're using Python 3) on your model.

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

Note, however, that you shouldn't be using that to_field_name attribute in the field definition. The default is to use the ID, which is the correct value to allow Django to set the foreign key correctly.

Upvotes: 4

GwynBleidD
GwynBleidD

Reputation: 20579

Label for choice is created in django forms by converting object into unicode (python 2.x) or string (python 3.x). If you want to display teamname here, just create __unicode__ (python 2.x, also you can create __str__ method and decorate object with python_2_unicode_compatible imported from django.utils.encoding so it will be compatible both with python 2.x and 3.x) or __str__ (python 3.x) method that will return string to display as choice label.

Upvotes: 2

Related Questions