0

this is my django view.What it basically does is, it gets the checked data from Html view.But I have to loop through each data in the view, or in template so that I can get not only the name of subtest but also it's fields.Subtest is the name of my model,and name is it's field

def create_test_bills(request):
    if request.method == 'GET':
        selected = request.GET.getlist('selected')
        for i in range(0,len(selected)):
            a = selected [i]
            print(selected)
            print(a)
            sub_test = Subtest.objects.filter(name=a)
    return render(request,'report.html',{'sub_test':sub_test}) 

1 Answer 1

1

you can use django filter __in https://docs.djangoproject.com/en/3.0/ref/models/querysets/#in

instead of looping through it:

def create_test_bills(request):
    if request.method == 'GET':
        selected = request.GET.getlist('selected')
        if len(selected) > 0:
           sub_test = Subtest.objects.filter(name__in=selected)
    return render(request,'report.html',{'sub_test':sub_test}) 
Sign up to request clarification or add additional context in comments.

4 Comments

I think you can skip the loop altogether, just use Subtest.objects.filter(name__in=selected)
@ruddra yeah i think so too i wrote like that as i'm not certain what getList return
it returns a list :)
oh, note taken :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.