Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if key exists in datastore without returning the object

I want to be able to check if a key_name for my model exists in the datastore. My code goes:

t=MyModel.get_by_key_name(c)
    if t==None:
        #key_name does not exist

I don't need the object, so is there a way (which would be faster and cost less resource) to check if the object exist without returning it? I only know the key name, not the key.

like image 809
user375348 Avatar asked Sep 01 '10 01:09

user375348


2 Answers

You can't avoid get_by_key_name() or key-related equivalents to check if a key exists. Your code is fine.

like image 135
moraes Avatar answered Sep 19 '22 11:09

moraes


The API talks about Model.all(keys_only=False) returning all the key names when keys_only is set to True

Look at the query that is fired for this, and then you can write a query similar to this but just for your object and see if any row is fetched or not.

like image 26
Rahul Avatar answered Sep 22 '22 11:09

Rahul