不能在同一个Python文件中"定义"两个数据库?

Cre*_*mbo 1 python google-app-engine

我刚刚开始学习Python,我正在慢慢掌握它,但我遇到了一个问题.

我正在尝试使用Google的App Launcher SDK创建一个包含文章的简单网站.

现在,当我在Google网站上关注小指南时一切正常,但现在我想创建另一个数据库:

class Articles(db.Model):
    title = db.TextProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)

这里没有错误.然后我尝试进行查询,获取所有信息并将其发布到模板中:

class Articles(webapp.RequestHandler):
    def get(self):
        articles_query = Articles.all().order('-date')
        articles = articles_query.fetch(10)

        template_values = {'articles': articles}

        path = os.path.join(os.path.dirname(__file__), 'articles.html')
        self.response.out.write(template.render(path, template_values)) 
Run Code Online (Sandbox Code Playgroud)

在这里我收到一个错误:

line 45, in get
    articles_query = Articles.all().order('-date')
AttributeError: type object 'Articles' has no attribute 'all'
Run Code Online (Sandbox Code Playgroud)

我基本上从谷歌的教程中复制了查询,只是更改变量,但它不起作用.

有任何想法吗?

Bla*_*rad 7

这不是你定义了两个数据库,而是你试图创建两个叫做的类Articles.Python不能立刻将它们放在头脑中,所以一旦你做了class Articles(webapp.RequestHandler),它就会被替换掉class Articles(db.Model).

webapp.RequestHandler没有all()方法,你没有在第二个Articles类中定义一个.这就是为什么你得到你所做的特殊错误.

您应该为您的类使用不同的名称.