student101
student101

Reputation: 522

Python If statement in view using queryset

I'm attempting to use the following if statement which will display an empty set if the reportaccess is equal to All Reports using the following.

if reportaccess == 'All Reports':
    bhreportgrouplist = None
    cereportgrouplist = None
    finreportgrouplist = None
    careportgrouplist = None
    pireportgrouplist = None
    screportgrouplist = None
    dssreportgrouplist = None
    psgreportgrouplist = None
    othreportgrouplist = None

else:
    bhreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 200)
    cereportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 500)
    finreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 600)
    careportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 800)
    pireportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1100)
    screportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1200)
    dssreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1300)
    psgreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 1400)
    othreportgrouplist = QvReportList.objects.filter(~Q(report_id__in= reportIds)).filter(report_group_id = 99999)

When the view goes through it is always displaying the Else, but when I view the print of reportaccess it displays as :

 <QuerySet ['All Reports']>  

Why is my if condition not being met? How can I make it met

Upvotes: 0

Views: 284

Answers (1)

S_alj
S_alj

Reputation: 447

The check for if reportaccess == 'All Reports' does not return True because, from your print, reportaccess is a Queryset, and you are comparing it to a string, which will never be equal.

You can verify this by running

from django.db.models.query import QuerySet
isinstance(reportaccess, QuerySet)

It seems like what you want is to check if your Queryset reportaccess contains 'All Reports', so something like this:

something = 'All Reports'
if something in reportaccess:
    #do stuff

Upvotes: 1

Related Questions