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 trial

Python Using Databases in Python Meet Peewee Modeling

AttributeError: 'dict' object has no attribute 'create'

I have imported peewee, but I keep getting the error message.

def add_cookies(): for cookie in cookies: try: cookie.create(model=cookie['model'], model_number = cookie['model_number'], color = cookie['color'], year = cookie['year'], quantity = cookie['quantity'])
except IntegrityError: cookie_record = cookie.get(model=Bentley['model']) cookie_record.number = cookie['model_number'] cookie_record.year = cookie['year'] cookie_record.color = cookie['color'] cookie_record.quantity = cookie['quantity'] cookie_record.save()

Traceback (most recent call last): File "cookie.py", line 89, in <module> add_cookies() File "cookie.py", line 67, in add_cookiess cookie.create(model=cookie['model'], AttributeError: 'dict' object has no attribute 'create'

1 Answer

So first of all, have you created a class called Cookie somewhere in your code?
e.g.

class Cookie(Model) ?

The create() method is a method from the class Model, and so if you were to create the class Cookie it would inherit the create() method.

Then what you would want to say is more like

def add_cookies(): 
   for cookie in cookies: 
       try: Cookie.create(model=cookie['model'], model_number = cookie['model_number'], color = cookie['color'],
                          year = cookie['year'], quantity = cookie['quantity'])
       except IntegrityError: 
           cookie_record = Cookie.get(model=Bentley['model']) 
           cookie_record.number = cookie['model_number'] 
           cookie_record.year = cookie['year'] 
           cookie_record.color = cookie['color'] 
           cookie_record.quantity = cookie['quantity'] 
           cookie_record.save()