Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在尝试引用外键字段…为什么它还显示被引用表的名称?_Python_Django - Fatal编程技术网

Python 正在尝试引用外键字段…为什么它还显示被引用表的名称?

Python 正在尝试引用外键字段…为什么它还显示被引用表的名称?,python,django,Python,Django,我正在尝试显示模板中的数据。我遇到的问题是BattingStats表中的外键,该表引用了PlayerInfo表的playerid。当我让它显示时,该字段的数据会显示出来,但它被括在括号中,前面是ReferencedTable对象。所以对我来说,它看起来像是PlayerInfo对象(smithjo05)。它为什么要这样做?我如何让它只显示播放器ID?感谢您的帮助。谢谢 观点 模型 class BattingStats(models.Model): id = models.IntegerFi

我正在尝试显示模板中的数据。我遇到的问题是
BattingStats
表中的外键,该表引用了
PlayerInfo
表的
playerid
。当我让它显示时,该字段的数据会显示出来,但它被括在括号中,前面是
ReferencedTable对象
。所以对我来说,它看起来像是
PlayerInfo对象(smithjo05)
。它为什么要这样做?我如何让它只显示
播放器ID
?感谢您的帮助。谢谢

观点

模型

class BattingStats(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True)
    playerid = models.ForeignKey('PlayerInfo', models.DO_NOTHING, db_column='playerID', blank=True, null=True)
    player = models.CharField(db_column='Player', max_length=255, blank=True, null=True)
    hr = models.IntegerField(db_column='HR', blank=True, null=True)
    rbi = models.IntegerField(db_column='RBI', blank=True, null=True)
    ba = models.FloatField(db_column='BA', blank=True, null=True)

class PlayerInfo(models.Model):
    playerid = models.CharField(db_column='playerID', primary_key=True, max_length=255)
    namefirst = models.CharField(db_column='nameFirst', max_length=255, blank=True, null=True)
    namelast = models.CharField(db_column='nameLast', max_length=255, blank=True, null=True)
    height = models.IntegerField(blank=True, null=True)
    debut = models.CharField(max_length=255, blank=True, null=True)
    finalgame = models.CharField(db_column='finalGame', max_length=255, blank=True, null=True)
HTML

您只需将方法添加到
PlayerInfo
模型:

class PlayerInfo(models.Model):
    playerid = models.CharField(db_column='playerID', primary_key=True, max_length=255)
    namefirst = models.CharField(db_column='nameFirst', max_length=255, blank=True, null=True)
    namelast = models.CharField(db_column='nameLast', max_length=255, blank=True, null=True)
    height = models.IntegerField(blank=True, null=True)
    debut = models.CharField(max_length=255, blank=True, null=True)
    finalgame = models.CharField(db_column='finalGame', max_length=255, blank=True, null=True)

    def __str__(self):
        return self.playerid
这允许在将实例转换为字符串(例如在模板中)时更改其默认表示形式

            {% for index in playerstats %}
                <td>{{ index.playerid }}</td>
                <td>{{ index.year}}</td>
                <td>{{ index.age}}</td>
                <td>{{ index.team}}</td>
                <td>{{ index.league}}</td>
            {% endfor %}

{% for index in playerstats %}
    {{ index.playerid }}
{% endfor %}
PlayerInfo object (smithjo05) 
PlayerInfo object (cruzne02)  
PlayerInfo object (jonesad01)
class PlayerInfo(models.Model):
    playerid = models.CharField(db_column='playerID', primary_key=True, max_length=255)
    namefirst = models.CharField(db_column='nameFirst', max_length=255, blank=True, null=True)
    namelast = models.CharField(db_column='nameLast', max_length=255, blank=True, null=True)
    height = models.IntegerField(blank=True, null=True)
    debut = models.CharField(max_length=255, blank=True, null=True)
    finalgame = models.CharField(db_column='finalGame', max_length=255, blank=True, null=True)

    def __str__(self):
        return self.playerid