python


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:
...

Related Links

Why does the decorator module force me to lift the inner function to the outer level?
finding email address in a web page using regular expression
Python code giving error
Celery concurrency configuration for io/cpu bound task
Function that takes 3 list arguments and returns all the combinations [duplicate]
how to read files with special characters in python
Using the Python Request Library to get Google Oauth2 tokens
(Beginner)Python functions Codeacademy
Python script hangs
How to shift a list nth time in Left or Right and fill it up with 0 in python
NumPy, why equality check does not work for an array of objects?
String formatting of timedeltas in Pandas
Celery - How to send task from remote machine?
YouTube Api v3 handling Exceptions in Python
Copy data from one oracle database to another with Python
converting python program into executable

Categories

HOME
sidekiq
jenkins-plugins
shopify
xamarin.android
jsviews
json-ld
label
mapbox-gl-js
concourse
bibtex
orientation
file-io
jpanel
urlencode
codeblocks
gimp
aruco
google-plus
getorgchart
game-physics
rebol2
alfresco-share
redux-observable
google-openid
thumbnails
plupload
android-fragmentactivity
shippo
cruisecontrol.net
multiplayer
classpath
positioning
devforce
ivy
jboss5.x
piecewise
android-vpn-service
pari
pyscripter
freebase
polymorphism
viewstate
android-download-manager
code-behind
deepstream.io
fax
productivity
azure-availability-set
sgmlreader
restivejs
gradle-script-kotlin
iostat
backstop.js
multifile-uploader
dbscan
string-parsing
concur
sun-codemodel
p-np
base32
connection-reset
ubercart
criteria-api
coypu
cpu-cores
rhel5
hidden-field
funq
power-law
coin-flipping
pic24
mimosa
gfs
exiv2
angulartics
magicalrecord-2.2
backbone-relational
client-side-scripting
arbor.js
zend-framework-modules
cosm
deploying
z-machine
makefile-project
cross-domain-policy
zookeeper
curljs
phpcrawl
j-interop
pci-bus
savestate
usability-testing
revision
longjmp
data-entry
rd

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App