Hi,
I am very new to python and after a brief intro on to python + Google app engine, I've started to work on a pilot project. I have bulkloaded 2 entities UserDetails and PhoneBook with data onto the app engine. Now in my UI I try to first take in the user name then query it to get the user id from UserDetails, then using the retrieved user id I query the PhoneBook to get the his phone book entries. Here's my code for the UI,
#!/usr/bin/env python
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from models import UserDetails,PhoneBook
#import models
class showPhoneBook(db.Model):
""" class to store the user_name to db """
user_name = db.StringProperty(required=True)
class MyHandler(webapp.RequestHandler):
def get(self):
""" Query type 1 """
#p = db.GqlQuery('SELECT * FROM UserDetails WHERE user_name = :1', user_name)
""" Query type 2 """
#p = UserDetails.gql('WHERE user_name = :1', user_name)
""" Query type 3 """
p = UserDetails.all().filter('user_name = ', user_name)
result1 = p.get()
for itr1 in result1:
userId = itr.user_id
""" Query type 1 """
#q = db.GqlQuery('SELECT * FROM PhoneBook WHERE user_id = :1', userId)
""" Query type 2 """
#q = PhoneBook.gql('WHERE user_id = :1', userId)
""" Query type 3 """
q = PhoneBook.all().filter('user_id = ', userId)
values = {
'phoneBookValues': q
}
self.response.out.write(
template.render('phonebook.html', values))
def post(self):
phoneBookuserID = showPhoneBook(
user_name = self.request.get('username'))
phonebookuserID.put()
self.redirect('/')
def main():
app = webapp.WSGIApplication([
(r'.*',MyHandler)], debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__ == "__main__":
main()
The problem is that I get this error in my logs that GAE can't import the UserDetails/phone class from models,
<type 'exceptions.ImportError'>: cannot import name UserDetails
Traceback (most recent call last):
File "/base/data/home/apps/bulkloader160by2/1-4.337299749289926105/main.py", line 7, in <module>
from models import UserDetails,PhoneBook
and when I remove from models import UserDetails,PhoneBook
and use import models
, I get,
global name 'UserDetails' is not defined
Traceback (most recent call last):
File "/base/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 507, in __call__
handler.get(*groups)
File "/base/data/home/apps/bulkloader160by2/1-4.337300095868541686/main.py", line 19, in get
p = UserDetails.all().filter('user_name = ', user_name)
NameError: global name 'UserDetails' is not defined
This is my models.py file where I've my UserDetails and PhoneBook classes defined. I've stored this file in the root directory.
#!/usr/bin/env python
from google.appengine.ext import db
#Table structure of User Details table
class UserDetails(db.Model):
user_id = db.IntegerProperty(required = True)
user_name = db.StringProperty(required = True)
mobile_number = db.PhoneNumberProperty(required = True)
#Table structure of Phone Book table
class PhoneBook(db.Model):
contact_id = db.IntegerProperty(required=True)
user_id = db.IntegerProperty(required=True)
contact_name = db.StringProperty(required=True)
contact_number = db.PhoneNumberProperty(required=True)
Being new to python I am not able to figure out why am not able to import my classes from my models.py file and how to set/get the user_name from post(self)
to get(self)
.
Please forgive me if I'm being naive but your help is most appreciated if you can help me in setting my code right.
Thanks.