Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

filter foreign fileds django_filters

# *********** how to filter queryset with django foreign keys **********

# in models.py file
class BookedEvent(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    event = models.ForeignKey(Events, on_delete=models.CASCADE)

    
# in filters.py file
import django_filters
class BookedEventFilter(django_filters.FilterSet):
    class Meta:
        model = BookedEvent
        #use __ (double underscore) to target foreign key values
        fields = ['event__eventName', 'event__startDate','event__endDate','event__address']   

        
# in views.py file 
def booked_event_page_view(request):
   # getting queryset
    currentUser = request.user
    bookedEvents = models.BookedEvent.objects.filter(user=currentUser)
    # adding event filter
    filter = BookedEventFilter(request.GET, queryset=bookedEvents)
    bookedEvents = filter.qs
    context = {'bookedEvents': bookedEvents, 'filter':filter}
    return render(request, 'booked_events.html', context)
Comment

PREVIOUS NEXT
Code Example
Python :: How to install packages offline? Ask Question 
Python :: opencv load image python 
Python :: how to search for a data in excel pandas 
Python :: pytorch calculate mse mae 
Python :: python swap numbers 
Python :: A Python Class Constructor 
Python :: data where values in column starts with particular value 
Python :: resize cmd using python 
Python :: radians in python turtle 
Python :: print labels on confusion_matrix 
Python :: pip matplotlib 
Python :: python list join array string space 
Python :: how to make an array in python 
Python :: Python program to count Even and Odd numbers using lambda 
Python :: if string in list py 
Python :: python dict keys to string 
Python :: Shapes (None, 1) and (None, 5) are incompatible 
Python :: ConfusionMatrixDisplay size 
Python :: autopytoexe 
Python :: python lowercase first letter 
Python :: python filter() 
Python :: funcions in python 
Python :: muliline comment in pyhton 
Python :: def python 
Python :: multiple plot in one figure python 
Python :: raw input py 
Python :: python multiply 2 variables 
Python :: device gpu pytorch 
Python :: display pandas dataframe with border 
Python :: fast output python 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =