stackoverflow社区的您好,
我有以下两个模型定义如下(使用python manage.py inspectdb创建):
class StraightredFixture(models.Model):
fixtureid = models.IntegerField(primary_key=True)
soccerseason = models.IntegerField(db_column='soccerSeason') # Field name made lowercase.
home_team = models.ForeignKey('straightred.StraightredTeam', db_column='hometeamid', related_name='home_fixtures')
away_team = models.ForeignKey('straightred.StraightredTeam', db_column='awayteamid', related_name='away_fixtures')
fixturedate = models.DateTimeField()
fixturestatus = models.CharField(max_length=24)
fixturematchday = models.IntegerField()
hometeamscore = models.IntegerField()
awayteamscore = models.IntegerField()
class Meta:
managed = False
db_table = 'straightred_fixture'
class StraightredTeam(models.Model):
teamid = models.IntegerField(primary_key=True)
teamname = models.CharField(max_length=36)
teamcode = models.CharField(max_length=5)
teamshortname = models.CharField(max_length=24)
class Meta:
managed = False
db_table = 'straightred_team'
然后我有一个观点如下:
def jsonfixture(request):
data = StraightredFixture.objects.filter(fixturematchday=12)
json_data = serializers.serialize('json', data)
return HttpResponse(json_data, content_type='application/json')
这很好用,并产生如下可用的结果:
[{"fields": {"awayteamscore": 2, "hometeamscore": 1, "home_team": 70, "away_team": 328, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136932}, {"fields": {"awayteamscore": 1, "hometeamscore": 2, "home_team": 65, "away_team": 72, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136930}, {"fields": {"awayteamscore": 0, "hometeamscore": 0, "home_team": 338, "away_team": 71, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 137852}, {"fields": {"awayteamscore": 1, "hometeamscore": 2, "home_team": 62, "away_team": 563, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136929}, {"fields": {"awayteamscore": 0, "hometeamscore": 2, "home_team": 61, "away_team": 74, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136928}, {"fields": {"awayteamscore": 0, "hometeamscore": 1, "home_team": 67, "away_team": 69, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T15:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136931}, {"fields": {"awayteamscore": 2, "hometeamscore": 1, "home_team": 57, "away_team": 66, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-22T17:30:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136927}, {"fields": {"awayteamscore": 1, "hometeamscore": 3, "home_team": 354, "away_team": 64, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-23T13:30:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136926}, {"fields": {"awayteamscore": 2, "hometeamscore": 1, "home_team": 322, "away_team": 73, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-23T16:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136925}, {"fields": {"awayteamscore": 1, "hometeamscore": 1, "home_team": 58, "away_team": 340, "fixturematchday": 12, "soccerseason": 354, "fixturedate": "2014-11-24T20:00:00", "fixturestatus": "FINISHED"}, "model": "straightred.straightredfixture", "pk": 136924}]
唯一的问题是它正在返回" home_team"和" away_team" id而不是团队名称,这是期望的结果。
以前我用过:
fixture = StraightredFixture.objects.get(fixtureid=136697)
return render(request,'straightred/test.html',{'name':fixture.away_team.teamname})
这很好用,让我可以看到团队名称,但我不知道如何把这两个概念合二为一。
经过一些研究,我认为我可以简单地使用" .select_related"选项,但它似乎没有改变我的输出。
对此的任何帮助将不胜感激。
非常感谢,艾伦。
答案 0 :(得分:1)
您需要在模型中定义serialize
方法,并在class StraightredTeam(models.Model):
teamid = models.IntegerField(primary_key=True)
teamname = models.CharField(max_length=36)
teamcode = models.CharField(max_length=5)
teamshortname = models.CharField(max_length=24)
def natural_key(self):
return self.teamname
class Meta:
managed = False
db_table = 'straightred_team'
灯具时设置标志,示例如下:
use_natural_foreign_keys=True
然后在序列化时,只需设置标记json_data = serializers.serialize('json', data, use_natural_foreign_keys=True)
:
teamname
它应该将外键ID序列化为I
。