Problem in importing classes from models file in GAE
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 impor开发者_JAVA技巧t 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.
The first error you're getting occurs when it can import the module ('models') fine, but can't find the member ('UserDetails') in it. This would generally be the case if you'd mistyped the name of the class, or hadn't defined it in that module.
The second error is expected, because you're importing the 'models' module, but then trying to refer to UserDetails by its unqualified name - you need to refer to it as 'models.UserDetails' if you import just the module.
Based on the code you've provided, though, I can't see anything wrong, which leads me to believe it's something you've changed for the snippets you pasted. Double-check that UserDetails is defined correctly in your models.py file. You might also want to try the following:
import models
logging.warn(models.__file__)
This will show you the path to the models module, which will let you verify there's not another module by the same name somewhere that you're accidentally importing instead.
If models.py is in a subdirectory you might need to add it to the system path, such as:
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
If models is the directory and the classes are in UserDetails.py etc. you may need to:
sys.path.append(os.path.join(os.path.dirname(__file__), "models"))
import UserDetails
精彩评论