Articles → DJANGO → Different Functions Of Queryset In Django

Different Functions Of Queryset In Django






What Is Queryset?





Functions




  1. Filter
  2. Update
  3. Exclude


Picture showing the view populated using the get_list method
Click to Enlarge


Filter




def get_list(request):
    template_name = "get_list.html"
    queryset = MyNewScreen.objects.all()
    queryset = queryset.filter(name="test123")
    print(queryset.count())
    context = {"list": queryset}
    return render(request, template_name, context)


Picture showing the list of records after the filter is applied on it
Click to Enlarge


Update




def get_list(request):
    template_name = "get_list.html"
    queryset = MyNewScreen.objects.all()
    queryset.update(name="test567")
    print(queryset.count())
    context = {"list": queryset}
    return render(request, template_name, context)




Picture showing the list of records once the data is updated
Click to Enlarge


Exclude




def get_list(request):
    template_name = "get_list.html"
    queryset = MyNewScreen.objects.all()
    queryset = queryset.exclude(name="test44")
    print(queryset.count())
    context = {"list": queryset}
    return render(request, template_name, context)


Picture showing the list of records after the execution of exclude function
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, January 13, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250