how to filter queryset with foreign key in django

Solutions on MaxInterview for how to filter queryset with foreign key in django by the best coders in the world

showing results for - "how to filter queryset with foreign key in django"
Andrea
02 Aug 2017
1# *********** how to filter queryset with django foreign keys **********
2
3# in models.py file
4class BookedEvent(models.Model):
5    user = models.ForeignKey(User, on_delete=models.CASCADE)
6    event = models.ForeignKey(Events, on_delete=models.CASCADE)
7
8    
9# in filters.py file
10import django_filters
11class BookedEventFilter(django_filters.FilterSet):
12    class Meta:
13        model = BookedEvent
14        #use __ (double underscore) to target foreign key values
15        fields = ['event__eventName', 'event__startDate','event__endDate','event__address']   
16
17        
18# in views.py file 
19def booked_event_page_view(request):
20   # getting queryset
21    currentUser = request.user
22    bookedEvents = models.BookedEvent.objects.filter(user=currentUser)
23    # adding event filter
24    filter = BookedEventFilter(request.GET, queryset=bookedEvents)
25    bookedEvents = filter.qs
26    context = {'bookedEvents': bookedEvents, 'filter':filter}
27    return render(request, 'booked_events.html', context)
queries leading to this page
if a foreign key is used how to filter by that foreign key djangodjango filters foreign keydjango filter foreign key attributehow to filter queryset django with foreign keydjango filter with foreign keydjango query 22 filter 22 by field in foreign tabledjango filter by foreign keydjango foreign key filterhow to filter foreign key in djangoapply filter on foreign key fields in django viewhow to filter with foreign key in djangodjango foreign key filteredapply the filter on foreign key djangofilter using foreignkey model field in djangofilter foreign fileds django filtersdjango queryset filter reverse foreign keydjango filters filterset foreign keyhow to filter a foreign key in djangoforeign key filter in djangofilter foreign key djangodjango model foreign key filter querysetfilter the queryset for a foreign key field in django modelsdjango filters filterset foreign key all django query filter on foreign keyforeign key queries filter in djangodjango filter custom filter foreign key alldjango queryset filter by foreign keyfiltering foreign key djangodjango filter foreign key iddjango filter on foreign keydjango filter foreign key valuehow to filter a model using foreign key djangodjango model filter by foreign key fielddjango query filter by field in foreign tablehow to filter ann item from its foreign key in djangohow to filter based on foreign key in djangodjango filter form field from foreign keyfilter foreign key django get filter on foreign key djangofilter by foreign key id djangohow to filter on foreign key attrinute djangoapply filter on foreign key field in django viewdjango filter filter by foreign key propertydjango objects filter by foreign key valuesdjango filter on foreign key valueforeign key filter djangodjango filter foreignkey properyhdjango query 22filter by field in foreign tablehow to use filter in django model foreign keyon basis of foreign key filter in djangohow to filter by foreign key djangohow to filter in model py foreign key djangodjango foreignkey filtersetdjango filters foreign key fieldfilter foreign key django modelsdjango filter foreign key fielddjango filter foreign key formdjango filter for model containing the foreign keyhow to write django model with foreign key in filterdjango relationship filter foreign keydjango filters foreign keyaccess foreign key field django model filterdjango model foreign key filterfilter with foreign djangodjango filter fk queryhow to create django filter by foreign key fielddjango queryset filter by field in foreign tabledjango models filter foreign keyforeign key django filterdjango filter foreign keyfilter by foreign key djangodjango filter in foreign key modelsdjango query filter by foreign keydjango objects filter foreign keydjango filter foreign key related namefilter foreign key options based on user in djangohow to use django filters on foreign key relateddjango query filter foreign key fielddjango filter queryset foreign keyhow to filter queryset with foreign key in django