[Answered ]-Accessing matching entry of two models using ForeignKey

1👍

From the docs, with DetailView you can use self.object to get the Game instance:

While this view is executing, self.object will contain the object that the view is operating upon.

You can then filter like this:

    def get_context_data(self, **kwargs):
        context = super(GameDetailView, self).get_context_data(**kwargs)
        context.update({
            'game_status': models.Relation.objects.filter(game=self.object)
        })
        return context

Or use that instance to get all related Relations by following the relationship backwards:

    def get_context_data(self, **kwargs):
        context = super(GameDetailView, self).get_context_data(**kwargs)
        context.update({
            'game_status': self.object.relation_set.all()
        })
        return context

Leave a comment