filter foreign fileds django filters

Solutions on MaxInterview for filter foreign fileds django filters by the best coders in the world

showing results for - "filter foreign fileds django filters"
Erika
07 Oct 2020
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
django queryset filter foreign keyfilter foreign key djangodjango query filter foreign key fielddjango filter queryset foreign keyhow to filter queryset django with foreign keyhow to filter on foreign key attrinute djangodjango query 22 filter 22 by field in foreign tabledjango queryset filter by field in foreign tableon basis of foreign key filter in djangodjango filter on foreign key valuehow to create django filter by foreign key fielddjango filter foreignkey properyhdjango query filter by field in foreign tablehow to use django filters on foreign key relateddjango filter filter by foreign key propertyhow to filter a model using foreign key djangodjango filter in foreign key modelsfilter foreign key django modelsdjango filters filterset foreign key all how to write django model with foreign key in filterfilter the queryset for a foreign key field in django modelsfiltering foreign key djangodjango objects filter foreign keydjango foreign key filteraccess foreign key field django model filterdjango filter foreign key fielddjango filter foreign key formhow to filter a foreign key in djangofilter with foreign djangoapply filter on foreign key fields in django viewdjango model foreign key filter querysethow to filter in model py foreign key djangoforeign key queries filter in djangofilter using foreignkey model field in djangodjango model filter by foreign key fieldhow to filter ann item from its foreign key in djangodjango filter foreign key idfilter foreign key django get filter foreign fileds django filtersdjango query filter on foreign keydjango filter foreign key valuedjango filter by foreign keyhow to use filter in django model foreign keyapply filter on foreign key field in django viewhow to filter objects in django not the foreign keyfilter by foreign key id djangofilter by foreign key djangofilter on foreign key djangodjango filter fk querydjango filter foreign keyhow to filter based on foreign key in djangodjango filter for model containing the foreign keydjango foreign key filtereddjango filters filterset foreign keyhow to filter by foreign key djangodjango filter custom filter foreign key alldjango filters foreign keydjango foreignkey filtersetdjango queryset filter by foreign keydjango filters foreign keydjango objects filter by foreign key valuesforeign key filter in djangodjango filter foreign key related nameforeign key django filterdjango models filter foreign keyapply the filter on foreign key djangodjango query 22filter by field in foreign tablehow to filter with foreign key in djangoforeign key filter djangodjango relationship filter foreign keyif a foreign key is used how to filter by that foreign key djangodjango filter foreign key attributehow to filter foreign key in djangodjango queryset filter reverse foreign keydjango filter form field from foreign keydjango query filter by foreign keydjango filters foreign key fielddjango filter by property of foreign keydjango filter with foreign keyfilter foreign key options based on user in djangodjango model foreign key filterdjango filter on foreign keyfilter foreign fileds django filters