0

TastyPie の出力に表示されるように、多数の ForeignKey 関係を取得しようとしています。これが私のモデルです:

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,

何か案は?

4

2 に答える 2

0

レポート内の反応を確認したい場合の問題に対する正しい答えは、 からリソースReportへの逆の ForeignKey を含めることです。Reaction

class ReportResource(ModelResource):
    reaction_set = fields.ToManyField('yourapp.resources.ReactionResource', 'reaction_set', full=False)
    class Meta:
        queryset = Report.objects.all()
        resource_name = 'report'

class ReactionResource(ModelResource):
    report = fields.ForeignKey(ReportResource, 'report', full=True, null=True)
    class Meta:
        queryset = Reaction.objects.all()
        resource_name = 'reaction'

この ToManyField は、reaction_setフィールド内のすべてのリアクション リソース URI を表示します。Resource がまだ宣言されていないことを考えると、Resource の名前は完全な文字列パスでなければなりません。それが役に立てば幸い。

于 2013-09-02T14:14:58.157 に答える