Check if value is already exists in ListProperty of Google Datastore Model in Python
I just start to learn python and I'm also new to Google Datastore Models. So please forgive me if this question looks too noob. What I'm trying to do is pretty simple. I'm validating if the user is already liked on a post. So I need to check if a key is inside the ListProperty of the Google Datastore Model. PFB for my code. #classmethod def likePost(cls,user_key): if user_key not in cls.liked_user: cls.liked_user.append(user_key) return True else: return False The cls is the "Post" object of the Model type from Google Datastore. 'liked_user' is the attribute type ListProperty of the "Post" object. But apparently, ListProperty is not iterable. Below is the error from console. TypeError: argument of type 'ListProperty' is not iterable. So, please help me to figure out how to achieve it. I tried googling for hours, still cannot figure out how to implement this one. Thanks in advance.
These properties don't apply to a model class, they apply to individual instances of that model. An instance represents an entity in the datastore, and it is this instance that has a list in its "liked_user" field. It makes no sense to ask if a value is in a class property. It's not quite clear what you are trying to do here. If you just want to know if a user has already liked another user, then it shouldn't be a classmethod at all; just a normal instance method, which takes the instance. # no decorator def likePost(self, user_key): if user_key not in self.liked_user: self.liked_user.append(user_key) return True else: return False If however you are trying to find out if any instance has that key in its liked_user, then you need to query the database: #classmethod def likePost(cls,user_key): users = cls.query(cls.liked_user == user_key).get() if users: ...
Scrapy - Use proxy middleware but disable proxy for specific requests
Why is pip installation of pyOpenSSL 0.13 failing?
Python Pandas - Add a new column with value based on first and last name in multiple columns
Filter in template to arrange data specifically in django
Retrieving data using Beautiful Soup
How can I get this series to a pandas dataframe?
Python - How to create a folder with a user entered name?
python win32com powerpoint ribbon xml
Read and write data to new file Python
How can I create a figure with optimal resolution for printing?
creating a for loop where xpath increases
Beautifulsoup - scraping everything but table data
Django-Haystack returns no results
How did I end up creating list from dataframe in Python Spark?
pandas: create single size & sum columns after group by multiple columns
How to check the shape of multiple arrays contained in a list?