user1470034
user1470034

Reputation: 691

Django queryset If statement never evaluates to true?

I have a simple update view. That reads the POST request from a previous view. This part works great.

owner = ADMirror.objects.get (employeentname=request.POST.get('userpost'))

I have a query set defined as:

currentlevel = QVReportAccess.objects.filter(ntname = 'owner.employeentname, active = 1).values('sr_datareduce_summary_code')

which the print looks like:

<QuerySet [{'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_c
ode': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_sum
mary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_dataredu
ce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_da
tareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {
'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z0712
6'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code':
'Z07126'}, '...(remaining elements truncated)...']>

The query set will have duplicates for the sr_datareduce_summary_code because they are at individual program levels in the model.

I then have the following if statement to give a yes if true and no if false, but even though the queryset contains Z07126 my if statement never evaluates to true. Why is this and how can I get it to work correctly?

if QVReportAccess.objects.filter(ntname = owner.employeentname, active = 1).values_list('sr_datareduce_summary_code') == "Z07126":
    appaccess = 'yes'
else:
    appaccess = 'no'

print(appaccess)

Upvotes: 0

Views: 1301

Answers (3)

Sumeet Kumar
Sumeet Kumar

Reputation: 1019

queryset = QVReportAccess.objects.filter(ntname=owner.employeentname, active=1).values_list('sr_datareduce_summary_code', flat=True)

if queryset.exists() and str(queryset[0]) == "Z07126":
    appaccess = 'yes'
else:
    appaccess = 'no'

print(appaccess)

so filter() basically return a queryset and not an object, it might also be blank, best way to check if queryset is empty or not is to use exists() values_list() returns tuples when iterated over. Each tuple contains the value from the respective field passed into it.

If you only pass in a single field, you can also pass in the flat parameter. If True, this will mean the returned results are single values, rather than one-tuples. Example when you don't pass flat=True:

>>> Entry.objects.values_list('id').order_by('id')
[(1,), (2,), (3,), ...]

when you pass flat=True:

>>> Entry.objects.values_list('id', flat=True).order_by('id')
[1, 2, 3, ...]

I hope you get the idea for solving your problem.

Upvotes: 1

Evhz
Evhz

Reputation: 9284

Or you can just loop the filtered Queryset:

for obj in QVReportAccess.objects.filter(
    ntname=owner.employeentname, 
    active=1, 
    sr_datareduce_summary_code="Z07126"):

    # do something with obj
    print(obj.sr_datareduce_summary_code)

Upvotes: 1

Sayse
Sayse

Reputation: 43330

You're comparing a list to a string, that'll never be true

Either you just need to check if that code exists in the filter

if QVReportAccess.objects.filter(ntname = owner.employeentname, active = 1, sr_datareduce_summary_code= "Z07126").exists():

or if you need to keep the values_list, assign it to a variable and use in with values_list('sr_datareduce_summary_code', flat=True)

if 'Z07126' in my_query_list:

Upvotes: 2

Related Questions