我试图让我的一些 ForeignKey 关系出现在我的 TastyPie 输出中。这是我的模型:
class Report(models.Model):
safetyreportid = models.SlugField("Safey Report Unique Identifier", max_length=125, primary_key=True)
safetyreportversion = models.IntegerField("Safety Report Version Number", max_length=125, blank=True, null=True)
primarysourcecountry = models.CharField("Country of the primary reporter", max_length=3, blank=True)
occurcountry = models.CharField("Country where the event occured", max_length=3, blank=True)
class Reaction(models.Model):
report = models.ForeignKey(Report)
reactionmeddrapt = models.CharField("MedDRA Preferred Term used to characterize the event", max_length=250, blank=True)
reactionmeddraversionpt = models.CharField("MedDRA version for reaction/event term PT", max_length=100, blank=True)
和我的 API.py 文件:
class ReactionResource(ModelResource):
class Meta:
queryset = Reaction.objects.all()
resource_name = 'reaction'
class ReportResource(ModelResource):
reaction = fields.ForeignKey(ReactionResource, attribute='reaction', full=True, null=True)
class Meta:
queryset = Report.objects.all()
resource_name = 'report'
但是,即使存在关系(我可以在 django 管理面板中看到),我在 JSON 输出中得到的只是:
reaction: null,
有任何想法吗?