I’ve just started learning Python and I’m slowly getting the hang of it, but I’ve faced a problem.
I’m trying to make a simple website with articles using Google’s App Launcher SDK.
Now, everything worked fine when I followed the little guide in Google’s site, but now I want to create another database:
class Articles(db.Model):
title = db.TextProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
No error here.
I then try to make a query, fetch all info and post it into a template:
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))
Here I received an error:
line 45, in get
articles_query = Articles.all().order('-date')
AttributeError: type object 'Articles' has no attribute 'all'
I basically copied the query from Google’s tutorial and just change the variables, yet it doesn’t work.
Any ideas?
It’s not that you’ve defined two databases, but that you’ve tried to create two classes called
Articles. Python can’t keep both of them in its head at once, so once you madeclass Articles(webapp.RequestHandler), it replacedclass Articles(db.Model).webapp.RequestHandlerdoesn’t have anall()method, and you’ve not defined one in the secondArticlesclass. That’s why you get the particular error that you do.You should use different names for your classes.