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 trialAndy Hughes
8,479 PointsStuck on error with ForeignKeyField
I am trying to follow along with the post model steps for the Social App. I'm doing it in VS Code because I want the benefits of more up-to-date code and extension. So far so good, but I've hit an error I can't seem to figure out.
Firstly let me post the error message:
Traceback (most recent call last):
File "app.py", line 8, in <module>
import forms
File "D:\Dropbox\Coding Projects\#######\forms.py", line 6, in <module>
from models import User
File "D:\Dropbox\Coding Projects\#######\models.py", line 45, in <module>
class Post(Model):
File "D:\Dropbox\Coding Projects\#######\models.py", line 47, in Post
user = ForeignKeyField(
TypeError: __init__() missing 1 required positional argument: 'model'
If I'm correct, the error is referring to the ForeignKeyField in my Post class? I have rewatched the videos, checked the syntax, scoured stackoverflow and looked at the WTForms & Flask docs. But I can't seem to find what's wrong. Hence the post.
Below is my code that seems to be throwing the error:
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',)
What I can say, is that if I comment out the user variable part, the app then loads and runs as expected (up to the part where it needs to submit the post.).
Part of me is wondering whether this is a database issue. It seems to me like maybe the form is not able to submit the post because it doesn't have the relevant tables to post too.
The below piece of code obviously creates the User table but how is the Post table created to store the posts?
def initialize():
DATABASE.connect()
DATABASE.create_tables([User], safe=True)
DATABASE.close()
Any clues or help would be greatly appreciated. :)
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsHey Andy Hughes, thanks for including the traceback. It holds the vital clue.
The trackbacks points to models.py, line 47 within Post definition.
The error is a missing positional argument. This means there is a parameter that must come before all keyword arguments and is not the name of any provided keyword argument.
A quick check of the documentation shows a model argument is missing. Perhaps, User
?
Could it be that rel_model=User
should just be User
?
Post back if you need more help. Good luck!!!
Andy Hughes
8,479 PointsAndy Hughes
8,479 PointsYes Chris Freeman , that was exactly it. I was looking at the wrong docs, I thought it was wtforms for some reason. As soon as I changed it to:
As per your suggestion, it started working ok. So I guess the question now in my head is "is this a python version thing? Or is it a Peewee code alteration? Or has Kenneth done something he shouldn't have, in his video? :P
Thanks again Chris. All the more reason not to just do these things in workspace! Things change :)