开发者

Unable to query from entities loaded onto the app engine datastore

开发者 https://www.devze.com 2022-12-09 21:01 出处:网络
I am a newbie to python. I am not able to query from the entities- UserDetails and PhoneBook I loaded to the app engine datastore. I have written this UI below based on the youtube video by Brett on \

I am a newbie to python. I am not able to query from the entities- UserDetails and PhoneBook I loaded to the app engine datastore. I have written this UI below based on the youtube video by Brett on "Developing and Deploying applications on GAE" -- shoutout application. Well I just tried to do some reverse engineering to query from the datastore but failed in every step.

#!/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
import models

class showPhoneBook(db.Model):
    """ property to store user_name from UI to persist for the session """ 
    user_name = db.StringProperty(required=True)

class MyHandler(webapp.RequestHandler):
    def get(self):
        ## Query to get the user_id using user_name retrieved from UI ##
        p = UserDetails.all().filter('user_name = ', user_name)
        result1 = p.get()
        for itr1 in result1:
            userId = itr.user_id
        ## Query to get the phone book contacts using user_id retrieved ##
        q = PhoneBook.all().filter('user_id = ', userId)
        values = {
            'phoneBookValues': q
        }
        self.request.out.write(
            template.render('phonebook.html', values))
    def post(self):
        phoneBookuser = showPhoneBook(
            user_name = self.request.get('username'))
        phoneBookuser.put()
        self.redirect('/')

def main():
    app = webapp.WSGIApplication([
        (r'.*',MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()

This is my models.py file where I've defined my UserDetails and PhoneBook classes,

#!/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)

Here are the problems I am facing,

1) I am not able to call user_name (retrieved from UI-- phoneBookuser = showPhoneBook(user_name = self.request.get('username'))) in get(self) method for querying UserDetails to to get the corresponding user_name.

2) The code is not able to recognize UserDetails and PhoneBook classes when importing from models.py file.

3) I tried to define UserDetails and PhoneBook classes in the main.py file itself, them I get the error at result1 = p.get() saying BadValueError: Unsupported type for property : <class 'google.appengine.ext.db.PropertiedClass'>

I have been struggling since 2 weeks to get through the mess I am into but in vain. Please help me out in straightening out my code('coz I feel what I've written is a error-prone code all the way).


I recommend that you read the Python documentation of GAE found here.

Some comments:

  1. To use your models found in models.py, you either need to use the prefix models. (e.g. models.UserDetails) or import them using

    from models import *

  2. in MyHandler.get() you don't lookup the username get parameter

  3. To fetch values corresponding to a query, you do p.fetch(1) not p.get()

  4. You should also read Reference properties in GAE as well. I recommend you having your models as:

    class UserDetails(db.Model):
        user_name = db.StringProperty(required = True)
        mobile_number = db.PhoneNumberProperty(required = True)
    
    #Table structure of Phone Book table
    class PhoneBook(db.Model):
         user = db.ReferenceProperty(UserDetails)
         contact_name = db.StringProperty(required=True)
         contact_number = db.PhoneNumberProperty(required=True)
    

    Then your MyHandler.get() code will look like:

    def get(self):
        ## Query to get the user_id using user_name retrieved from UI ##
        user_name = self.request.get('username')
        p = UserDetails.all().filter('user_name = ', user_name)
        user = p.fetch(1)[0]
        values = {
            'phoneBookValues': user.phonebook_set
        }
        self.response.out.write(template.render('phonebook.html', values))
    

    (Needless to say, you need to handle the case where the username is not found in the database)

  5. I don't quite understand the point of showPhoneBook model.


Your "session variable" being stored to the datastore isn't going to follow your redirect; you'd have to fetch it from the datastore in your get() handler, although without setting a session ID in a cookie or something this isn't going to implement sessions at all, but rather allow anyone getting / to use whatever value was send with a POST request whether it was sent by them or someone else. Why use the redirect at all; responding to a POST request should be done in the post() method, not through a redirect to a GET method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号