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 Build a Social Network with Flask Broadcasting Post Model

David Szemenkar
David Szemenkar
1,028 Points

TypeError in class Post(Model) - ForeignKeyField takes 2 arguments?

Hi, Having trouble with this code:

class Post(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField(rel_model=User,
    related_name='posts'
    )
    content = TextField()

    class Meta:
        database = DATABASE
        order_by = ('-timestamp',)

When I run it says:

"/FlaskSocial/models.py", line 44, in Post
    related_name='posts'
TypeError: __init__() takes at least 2 arguments (3 given)

Does anyone have an idea? Thanks in advance!

3 Answers

The ForeignKeyField in Peweee should look like the code below. It doesn't have parameters for rel_model, or related_name. LOOK at the documentation for the ORM that you are using.

user = ForeignKeyField(User, backref='tweets')

if you are using pewee, see link below. http://docs.peewee-orm.com/en/latest/peewee/api.html?highlight=ForeignKeyField#ForeignKeyField

David Szemenkar
David Szemenkar
1,028 Points

That works! Thank you very much!

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

This is due to breaking changes in peewee 3.0 (the video uses peewee 2). See the Backwards Incompatible list for full details.

The specific changes that are relevant here are that rel_model was renamed to model and related_name was renamed to backref.

Ben Hendricks
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Hendricks
Python Web Development Techdegree Student 15,217 Points

Treehouse needs to do an overlay on the video to show this. Would be really nice NOT to have to stumble through comment sections to find this type of thing...