1

I'm getting the incorrect output of a query set when I use request.method == 'POST' and selectedaccesslevel == '#' showing as <QuerySet ['S00009']> when it's written to the database.

I believe I should be using a get since i'm expecting one value, but how do I filter my get on coid = owner.coid.coid and provide the value ?level?

I'm looking for my output for datareducecode to be 'S00009' and not <QuerySet ['S00009']>. How can I accomplish this? Below is my view...

def submitted(request):



    owner = User.objects.get (formattedusername=request.user.formattedusername)
    checkedlist = request.POST.getlist('report_id')
    print (f"checkedlist on submitted:{checkedlist}")
    access_request_date = timezone.now()

    coid = User.objects.filter(coid = request.user.coid.coid).filter(formattedusername=request.user.formattedusername)
    datareducecode = OrgLevel.objects.distinct().filter(coid=request.user.coid.coid)
#    facilitycfo =  QvDatareducecfo.objects.filter(dr_code__exact = coid, active = 1, cfo_type = 1).values_list('cfo_ntname', flat = True)
#    divisioncfo =  QvDatareducecfo.objects.filter(dr_code__exact = coid, active = 1, cfo_type = 2).values_list('cfo_ntname', flat = True)
#    print(facilitycfo)
#    print(divisioncfo)
    selectedaccesslevel = request.POST.get('accesslevelid')
    print (f"accesslevel:{selectedaccesslevel}")

    selectedphi = request.POST.get('phi')
    print (f"phi:{selectedphi}")

    print (owner.coid.coid)

    if request.method == 'POST' and selectedaccesslevel == '3':
        datareducecode = OrgLevel.objects.filter(coid__exact = owner.coid.coid).values_list('slevel', flat = True)
        print (datareducecode)
        if request.method == 'POST' and selectedaccesslevel == '4':
            datareducecode = OrgLevel.objects.filter(coid__exact = owner.coid.coid).values_list('blevel', flat = True)
            print (datareducecode)
            if request.method == 'POST' and selectedaccesslevel == '5':
                datareducecode = OrgLevel.objects.filter(coid__exact = owner.coid.coid).values_list('rlevel', flat = True)
                print (datareducecode)
                if request.method == 'POST' and selectedaccesslevel == '6':
                    datareducecode = OrgLevel.objects.filter(coid__exact = owner.coid.coid).values_list('dlevel', flat = True)
                    print (datareducecode)
                    if request.method == 'POST' and selectedaccesslevel == '7':
                        datareducecode = OrgLevel.objects.filter(coid__exact = owner.coid.coid).values_list('f"Z{Coid}"', flat = True)
                        print (datareducecode)
    else:
        datareducecode = 'No Match on Coid'
        print (datareducecode)

    for i in checkedlist:
        requestsave = QVFormAccessRequest(ntname = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid.coid, facility = owner.facility, title = owner.title
                                          ,report_id = i, accesslevel_id = selectedaccesslevel, phi = selectedphi , access_beg_date = access_request_date, datareducecode = datareducecode )
        requestsave.save()

#    print (datareducecode)
    return JsonResponse({'is_success':True})
student101
  • 512
  • 3
  • 6
  • 16
  • Have you tried printing `str(datareducecode)`? – zipa Jan 03 '18 at 15:59
  • just did, print str(datareducecode) ^ SyntaxError: invalid syntax Also when I try to use str(datareducecode) in requestsave = it writes to the DB as – student101 Jan 03 '18 at 16:09
  • Okay, then you can go with `list(datareducecode)[0]` according to [this answer](https://stackoverflow.com/a/4424460/5811078). – zipa Jan 03 '18 at 16:21
  • That worked, if you write up an answer i'll mark it correct. – student101 Jan 03 '18 at 16:23
  • Just did, thanks in advance :) – zipa Jan 03 '18 at 16:25
  • I ran a test and the only if statement that occurs correctly is the first if request.method == 'POST' and selectedaccesslevel = '3'. When I select any of the other options 4,5,6, or 7 it writes a 'N' to the database. – student101 Jan 03 '18 at 16:35

1 Answers1

0

So this seems to do the trick:

list(datareducecode)[0]
zipa
  • 27,316
  • 6
  • 40
  • 58
  • This will only work for the first statement in the if statement. I dont' think these two issues are related list(datareducecode)[0] should work once the other issue is corrected. – student101 Jan 03 '18 at 16:39
  • That means that it is returning some string that begins wit `N`. Can you test it without `list`? – zipa Jan 03 '18 at 16:46
  • The issue was my if statement, i didn't have to nest it. – student101 Jan 03 '18 at 16:51