无法创建GAE实体

时间:2013-01-01 21:53:11

标签: python google-cloud-datastore typeerror keyword-argument

当我尝试在GAE中提交新实体时,收到以下错误消息。

File "C:\Users\Chris\Documents\Web Apps\legalstudybuddy\main.py", line 179, in post
    c = Courses(user=user, title=title)
TypeError: __init__() got an unexpected keyword argument 'user'

这是我的Python代码:

class Courses(db.Model):
    user = db.StringProperty(required = True)
    title = db.StringProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)

class Courses(Handler):
    def get(self, courses="", title="", signup_username="", login_logout=""):

        if not self.user:
            self.redirect('/login')

        user = str(self.read_secure_cookie('user_id'))
        courses = db.GqlQuery('SELECT * from Courses  WHERE user = :user ORDER BY created DESC', user=user)

        signup_username, login_logout = self.user_check()

        self.render('courses.html', courses=courses,
                    title=title,
                    signup_username=signup_username,
                    login_logout=login_logout)

    def post(self):
        if not self.user:
            self.redirect('/login')

        user = str(self.read_secure_cookie('user_id'))
        title = self.request.get('title')

        if title:
            c = Courses(user=user, title=title)
            c.put()

        self.redirect('/courses')

意外关键字参数来自创建实体而不是来自django模板。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您要定义Courses两次。一旦成为模特:

class Courses(db.Model):
    user = db.StringProperty(required = True)
    title = db.StringProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)

但也作为处理程序:

class Courses(Handler):
    # code

由于您定义了第二个处理程序,因此在当前命名空间中存储为Courses。例如:

In [1]: class Courses(object):
   ...:     a = 'First Courses'
   ...:     
   ...:     

In [2]: class Courses(object):
   ...:     a = 'Second Courses'
   ...:     
   ...:     

In [3]: Courses.a
Out[3]: 'Second Courses'

我建议更改处理程序的名称,以便不会遇到您现在遇到的冲突(当您调用Courses(user=user)时,它会尝试调用处理程序而不是模型)。