Django reverse lookup of foreign keys Django reverse lookup of foreign keys python python

Django reverse lookup of foreign keys


You can use events = venue.event_set to go the other way.

Note that venue.event_set is a manager object, like Event.objects, so you can call .all, .filter, .exclude and similar on it to get a queryset.

See the Django documentation


To those who have "'RelatedManager' object is not iterable"

Add all to retrieve the elements from the manager.

{% for area in world_areas.all %}

https://stackoverflow.com/a/16909142/2491526(cannot add this in comment to the first answer)


Go the other way round. Use Event model.

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

PS: I have never used get_object_or_404(). Modify code accordingly.