'bool' object is not subscriptable while trying to get value from queryset

I am trying to get a value from a queryset, but i keep getting this error that says 'bool' object is not subscriptable.

I have a queryset that looks like this tax_rate_fee = TaxRate.objects.filter(country=cartorder.country).exists() and i am checking to see if a specific country exists in the Queryset, then i want to grab a value from the queryset and perform some operation.

This is my code

tax_rate_fee = TaxRate.objects.filter(country=cartorder.country).exists()
if tax_rate_fee:
    cartorderitem.vat = 5 * tax_rate_fee['rate']

this is the tax rate fee model

class TaxRate(models.Model):
    country = models.CharField(max_length=200)
    rate = models.IntegerField(default=5, help_text="Numbers added here are in percentage (5 = 5%)")
    active = models.BooleanField(default=True)

You are not getting a queryset.

See QuerySet API reference | Django documentation | Django

From this, it seems i am not returning a queryset, but instead checking to find whether a queryset contains any items.
I also printed the tax_rate_fee and got back True.
So how do i check if an item exist and also grab a value from the queryset.

this is what i tried doing now: Check if the len() of tax_rate_fee queryset is equal to 1, then run the command.

tax_rate_fee = TaxRate.objects.filter(country=cartorder.country)
if len(tax_rate_fee) == 1:
   cartorderitem.vat = main_total * int(tax_rate_fee['rate'])

I then got this error QuerySet indices must be integers or slices, not str. that is why i tried converting the tax_rate_fee to integer here int(tax_rate_fee['rate']).

It seems i’m doing/getting it all wrong.

A queryset is a list of objects that are returned by the query. Even if a queryset only contains one items, it’s still a list.

That means the variable you are setting, tax_rate_fee is not an instance of TaxRate, it’s a queryset containing instance(s) of TaxRate.

that meas i can still check if the item exists in the queryset like this if tax_rate_fee.exists():


this was how i later did t

              tax_rate_fee = TaxRate.objects.filter(country=cartorder.country)
                # print( tax_rate_fee)
                if tax_rate_fee.exists():
                    print( tax_rate_fee)
                    cartorderitem.vat = main_total * tax_rate_fee.first().rate / 100
                    cartorderitem.vat = main_total * 0.01

thanks alot because this helped me fix it