Django_filter: filtering using current value of another field

So I was tasked with creating a events page where my company can post upcoming events and visitors can filter based on location, title or event type based on other 'is_virtual' which can be in person, virtual or hybrid. Now they want to 'ignore' the location filter if the event is virtual or hybrid but to keep it if the event is in person. I've been having issues accessing the value of the is_virtual field of the current object to create the conditional filtering based on it's value.

here's the filter code:

import django_filters
from django import forms
from .models import EventsPage

class EventsFilter(django_filters.FilterSet):
  event_type = django_filters.ChoiceFilter(
    choices=EventsPage.event_choices, 
    empty_label='Event Type',
    widget=forms.Select(attrs={'class': 'custom-select mb-2'})
  )

  location = django_filters.ChoiceFilter(
    choices=EventsPage.location_choices, 
    empty_label='All Locations',
    widget=forms.Select(attrs={'class': 'custom-select mb-2'})
  )

  title = django_filters.CharFilter(
    lookup_expr='icontains', 
    widget=forms.TextInput(
      attrs={'class': 'form-control', 'placeholder': 'Search Events'})
  )
  
  class Meta:
    model = EventsPage
    fields = ['event_type', 'location', 'title'] 

any ideas helps! thanks in advance.

tried using the method keyword argument built in django_filter library and the values passed to the helper method only show the value of the current field but I can't see the current value of the is_virtual field.

here's what I tried to do:

  location = django_filters.ChoiceFilter(
    method='location_filtering',
    choices=EventsPage.location_choices, 
    empty_label='All Locations',
    widget=forms.Select(attrs={'class': 'custom-select mb-2'})
  )
  def location_filtering(self, queryset, name, value):
    if (Q(virtual_choices='Virtual') | Q(virtual_choices='Hybrid')):
      return queryset.exclude(**{name: value})
    else:
      return queryset.filter(**{name: value})


Back to Top