Jay Littles
Jay Littles

Reputation: 5

How to return specific info in tuple from Django Queryset

I have a simple model with a tuple that is returning info as such below:

class Store(models.Model):
STORE_BRAND = (
    ('nike', 'Nike'),
    ('adidas', 'Adidas'),
    ('puma', 'Puma'),
)
online_store = models.CharField(unique=True, max_length=255, choices=STORE_BRAND)

def __str__(self):
    return self.online_store

I'm trying to return the store name so I can use it in a conditional statement in a context processor.

store_brand = Store.objects.get(online_store='nike')

Works fine, returns

<Store: nike> 

Now i'm trying to use it in a conditional statement and it keeps returning false:

>>> store_brand == 'nike'
False

What am I doing wrong?

Upvotes: 0

Views: 140

Answers (1)

Aman Garg
Aman Garg

Reputation: 2547

You are using the object to reference an attribute. You should use the correct attribute to refer to the value you want to access.

>>> store_brand = Store.objects.get(online_store='nike')

>>> store_brand.online_store == 'nike'  # the attribute online_store
True
>>> store_brand.__str__() == 'nike'  # since you defined __str__ to return the required attribute, you can use this too
True

Upvotes: 1

Related Questions