错误:
AttributeError at /stats/matches
Got AttributeError when attempting to get a value for field
players
on serializerMatchSerializer
. The serializer field might be named incorrectly and not match any attribute or key on theMatch
instance. Original exception text was: 'Match' object has no attribute 'players'.
型号:
每个Match
有10个玩家。在
class Match(models.Model):
tournament = models.ForeignKey(Tournament, blank=True)
mid = models.CharField(primary_key=True, max_length=255)
mlength = models.CharField(max_length=255)
win_rad = models.BooleanField(default=True)
class Player(models.Model):
match = models.ForeignKey(Match, on_delete=models.CASCADE)
playerid = models.CharField(max_length=255, default='novalue')
# There is also a Meta class that defines unique_together but its omitted for clarity.
序列化程序:
^{pr2}$
这里的问题是,
Match
模型没有一个名为players
的属性,请记住,您正在尝试获取向后关系对象,因此您需要使用djangodocs所说的players_set
作为字段。在你可以用两种方法来解决这个问题 1。向
PlayerSerializer
添加一个source
参数2。更改查找字段
^{pr2}$MatchSerializer
在Match
的实例中搜索players
属性,但找不到,并出现以下错误:在DRF序列化程序中,一个名为source的参数将显式地告诉您在哪里查找数据。因此,请按如下方式更改
^{pr2}$MatchSerializer
:希望有帮助。在
相关问题 更多 >
编程相关推荐