I am learning developing web app on google app engine, and I encounter a trouble.
I have some default users in my app and I write a function createDefaultUsers to create a User table using a list of their information in my python file. I hope to create this User table just once and every time I use my app, the data just there. But you know, the createDefaultUsers function is in my python file, so, very time I use my app, I just create this User table again and again. OGM~How can I fix this problem?
another question:
How can I clear my local datastore? It may help me debug my app, you know, sometime I just want to start again.
————–Update—————-
For example, here is some default users :
defaultUsers = [
('qingWANG','wang123456','wangqing@saad.com','teacher'),
('stevenYANG','123456','yifan@gmail.com','student'),
('jingZHU','zhu123456','zhujing@example.com','student'),
('conghuiHE','he123456','conghui@where.com','student'),
('lianDUAN','duan123456','duanlian@what.com','student'),
('xinHAO','hao123456','haoxin@example.com','student')]
and here is my function which create Users:
def createDefaultUsers():
"""
create default user table
"""
for user in defaultUsers:
users = Users(name = user[0], password = user[1],
email = user[2], role = user[3])
users.put()
In my first think, I put createDefaultUsers here:
def main():
createDefaultUsers()
run_wsgi_app(app)
if __name__ == "__main__":
main()
It is fine in my local testing. But when I deploy this, It doesn’t work (seems no Users created). where am I wrong?
You can easily modify the function to check to see if there are any existing users and if there are, don’t create them:
Do note that the
count()query operation is expensive because it actually retrieves all of the entities from the datastore in order to count them, so it is to be avoided in common practice, but by specifying a limit of 0, we can make it simple and quick in this case.You can empty out your development datastore by passing the command line argument
--clear-datastoreto the development server. Here are all ofdev_appserver.pycommand line arugments