Categories
Python Answers

How to do reverse lookup of foreign keys with Python Django?

Spread the love

To do reverse lookup of foreign keys with Python Django, we can use the filter method.

For instance, we write

def detail(request, venue_id):
    venue = Event.objects.filter(venue__id=venue_id)
    return render(request, 'venue-detail.html', {'venue': venue})

to look up the Event objects by the venue_id with filter.

And then we render the values in the venue-detail.html.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *