Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGerald Wells
12,763 Pointsattribute error peewee
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 4786, in <lambda>
names = lambda m: (m._meta.name, m._meta.db_table)
AttributeError: 'str' object has no attribute '_meta'
import datetime
from peewee import *
db = SqliteDatabase('diary.db')
class Entry(Model):
#content
content = TextField()
#timestamp
timestamp = DateTimeField(default=datetime.datetime.now) #no () at the end of now, if they were there it would cause datetime to be
#updated everytime the script ran
class Meta:
database = db
def initialize():
"""create a database and a table if they don't exist"""
db.connect()
db.create_tables(['Entry'], safe=True)
def menu_loop():
"""show menu loop"""
def add_entry():
"""Add an entry"""
def view_entry():
"""view previous entry"""
def delete_entry():
"""delete entry"""
if __name__ == '__main__':
initialize()
menu_loop()
Full stack trace:
Traceback (most recent call last):
File "diary.py", line 41, in <module>
initialize()
File "diary.py", line 21, in initialize
db.create_tables(['Entry'], safe=True)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 3448, in create_tables
create_model_tables(models, fail_silently=safe)
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 4766, in create_model_tables
for m in sort_models_topologically(models):
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 4787, in sort_models_topologically
for m in sorted(models, key=names, reverse=True):
File "/usr/local/lib/python3.4/dist-packages/peewee.py", line 4786, in <lambda>
names = lambda m: (m._meta.name, m._meta.db_table)
AttributeError: 'str' object has no attribute '_meta'
not sure what the error means and why I am getting it
Chris Freeman
Treehouse Moderator 68,441 PointsThe trackback started in the initialize()
function is a big hint. Then the error "AttributeError: 'str' object has no attribute '_meta'
", provides two clues:
-
'str' object as no attribute...
meaning something likely got a string it wasn't expecting -
...no attribute '_meta'
._meta
is a class-constructor attribute used bypeewee
(and *Django), so something was look for a class object with aMeta
information. More details in peewee docs model options and table metadata
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsThe error is in your initialize()
function. the method create_tables
takes Model
objects as arguments, not the string name of the model. Change to:
def initialize():
"""create a database and a table if they don't exist"""
db.connect()
db.create_tables([Entry], safe=True)
Gerald Wells
12,763 PointsThank you so much Chris!
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsCan you post the full stack trace? The error is sometimes not the first or last item listed. Thanks.