I'm currently playing with the Google App Engine datastore, and trying to extract some data from my applications database using GQL.
import os
import re
import random
import hashlib
import hmac
from string import letters
import webapp2
import jinja2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class Users(db.Model):
name = db.StringProperty(required = True)
password = db.StringProperty(required = True)
email = db.EmailProperty(required = True)
address = db.PostalAddressProperty(required = True)
join_date = db.DateTimeProperty(auto_now_add = True)
dob = db.DateTimeProperty()
phone = db.PhoneNumberProperty()
class MainPage(Handler):
def get(self):
self.render("signup-form.html")
def post(self):
name = self.request.get("username")
password = self.request.get("password")
verify = self.request.get("verify")
email = self.request.get("email")
address = self.request.get("address")
phone = self.request.get("phone")
a = Users(name = name, password = password, email = email, address = address, phone = phone)
a.put()
p = Users.all()
ans = p.filter("name = :1", "Manas")
if(ans.email == "[email protected]"):
self.response.write("Email found")
else:
self.response.write("Not found")
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
Now, there's already a 'Manas' attribute stored in my Users table, with '[email protected]' as its corresponding email id. So, accordingly, I should get a 'Email found' message displayed. However, I get this error:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "F:\Python 2.7\engineapp1\HelloApp\appapp\main.py", line 48, in post
ans = p.filter("name = :1", "Manas")
File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 2560, in filter
prop = match.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
What seems to be wrong with my query?
Query.filter
does not accept a placeholder.
Replace following line:
ans = p.filter("name = :1", "Manas")
with:
ans = p.filter("name =", "Manas")
UPDATE
filter
returns an iterable, not a single record. You need to iterate it to check whether given email exist:
for x in ans:
if x.email == "[email protected]":
self.response.write("Email found")
else:
self.response.write("Not found")
More preferably, using chained Query.filter
and Query.count
, you don't need to iterate it.
ans = p.filter("name =", "Manas").filter("email =", "[email protected]")
if ans.count(limit=1):
self.response.write("Email found")
else:
self.response.write("Not found")