向GAE数据库类添加方法

时间:2015-05-15 02:05:43

标签: google-app-engine

我正在搞乱GAE。我想将我的数据库对象放在一个文件中,然后从另一个文件中调用它。这是DB对象:

import webapp2
import os
import jinja2
import json
import logging
import main 



from google.appengine.ext import db


class User(db.Model):
    user_name = db.StringProperty(required = True)
    hashed_password = db.StringProperty(required = True)
    email = db.EmailProperty(required = True)
    created_dttm = db.DateTimeProperty(auto_now_add = True)
    last_modified = db.DateTimeProperty(auto_now = True)
    coords = db.GeoPtProperty(required = False)


    # def as_dict(self):
        # time_fmt = '%c'
        # d = { 
                # 'subject':self.subject,
                # 'content':self.content,
                # 'created':self.created_dttm.strftime(time_fmt),
                # 'last_modified': self.last_modified.strftime(time_fmt)
            # }
        # return d  

    def isValueUnique(self,column,value):
        result = None
        q = User.all()
        q.filter(column, value)
        result = q.get()
        return result

我无法实例化数据库,因为它认为我正在尝试存储数据。

我想从另一个文件中调用isValueUnique方法,如下所示:

import webapp2
import os
import jinja2
import json
import logging
import main 
import database
import validation

from google.appengine.ext import db



class SignUp(main.Handler):
    def post(self):
        user_username = self.request.get("username")
        user_email = self.request.get("email")
        user_pass = self.request.get("password")
        user_verify = self.request.get("verify")
        valid = validation.Valid()
        error1=""
        error2=""
        error3=""
        error4=""


        q = database.User.all()
        q.filter("username =", user_username)
        result = q.get()
        if result:
            error1="Username already taken"
        if (not valid.valid_user(user_username)) and (not error1):
            error1 = "Enter a valid username"
        if not valid.valid_password(user_pass):
            error2 = "Enter a valid password"
        if not valid.valid_pass_match(user_pass,user_verify):
            error3 = "Passwords must match"

        # Email Validation
        email=valid.valid_email(user_email)
        if not email:
            error4 = "Invalid email"
            email=""
        elif not database.User.isValueUnique("email",email):
            error4 = "Email already in use, please sign in"
            email=""

我收到此错误:     elif not database.User.isValueUnique(“email”,email):

TypeError:必须使用User实例作为第一个参数调用unbound方法isValueUnique()(改为使用str实例)

我不能像我已经说过的那样实例化用户。这里的工作是什么?

1 个答案:

答案 0 :(得分:1)

database.User.isValueUnique("email",email)

这是尝试在database.User类上调用方法,但isValueUnique实例方法。

如果用isValueUnique装饰@staticmethod,你就会越走越远。

您在哪里尝试实例化User