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

How to copy a file to a remote server in Python using SCP or SSH?
Change command Method for Tkinter Button in Python
Why do you need explicitly have the “self” argument into a Python method?
What's a good resource for starting to write a programming language, that's not context free? [closed]
Keeping filters in Django Admin
In GTK, how do I make a window unable to be closed?
Python: how can I handle any unhandled exception in an alternative way?
How to roll my own pypi?
Is there a way to check whether function output is assigned to a variable in Python?
Load non-uniform data from a txt file into a msql database
Python scoping problem
How can I strip comments and doc strings from python source code?
How do I display real-time python script output on a website?
how to extract some text by use lxml?
dynamically adding functions to a Python module
Scaling the y-axis with Matplotlib in Python

Categories

HOME
python
visual-studio-2015
sql-server
reverse-engineering
nlp
wmic
backup
agile
thunderbird-addon
ubuntu-12.04
is-empty
rocketmq
webdav
hyperledger-fabric
phaser-framework
rebol
file-io
triggers
i2c
structuremap
powershell-v3.0
window
line-api
ejs
spring-cloud-config
dendrogram
scenebuilder
factor-analysis
prestodb
multicore
ng2-dragula
jni
raphael
deb
kitematic
richfaces
source-maps
azure-servicebus-queues
pentaho-report-designer
edge-detection
data-conversion
multichoiceitems
cortex-m3
positioning
strophe
percentage
hotmail
fabric-digits
recurrence-relation
superpowered
von-neumann
passenger
mmdrawercontroller
pmwiki
parse-android-sdk
dql
silverlight-3.0
agent
equivalence
wsadmin
distributed-transactions
excel-interop
fastq
dojox.grid.datagrid
double-buffering
taskmanager
data-integration
amazon-machine-learning
gauss
nuget-server
pagefile
paas
jszip
collapsingtoolbarlayout
tomee
dac
logcat
mpeg-4
post-processing
insertion-sort
enyo
p-np
abas
achartengine
apache-mina
java-melody
cpu-speed
em
node-imagemagick
ember-addon
factors
gamepad
.net-cf-3.5
layered
sttwitter
localtime
fraud-prevention
gdataxml
dynamic-data
android-4.0
socketstream
subtract
signals2
nintendo
path-manipulation
nemerle
coff
windows-live-id
loadui
j-interop
external-assemblies
asp.net-mvc-controller
usability-testing
method-signature
gears
interface-design
associativity
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