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 trialRoneshia Allen
5,192 PointsI'm struggling with this Challenge.
I am getting this Error message. I'm not sure what's wrong.
import datetime
from flask.ext.bcrypt import generate_password_hash
from flask.ext.login import UserMixin
from peewee import *
DATABASE = SqliteDatabase(':memory:')
class User(UserMixin, Model):
email = CharField(unique=True)
password = CharField(max_length=100)
join_date = DateTimeField(default=datetime.datetime.now)
bio = CharField(default='')
class Relationship(Model):
from_user = ForeignKeyField(User, related_name='relationships')
to_user = ForeignKeyField(User, related_name='related_to')
class Meta:
database = DATABASE
@classmethod
def new(cls, email, password):
cls.create(
email=email,
password=generate_password_hash(password)
)
class LunchOrder(Model):
order = TextField()
date = DateField()
user = ForeignKeyField(User, related_name="orders")
def initialize():
DATABASE.connect()
DATABASE.create_tables([User, LunchOrder], safe=True)
DATABASE.close()
2 Answers
Peter Vann
36,427 PointsI'm not sure if this is your material issue, but I spot one problem with the code right off the bat. Python is very unforgiving about improper indentation since that's how code blocks are designated (and not with curly braces like in JavaScript).
So this part of your code:
class Relationship(Model):
from_user = ForeignKeyField(User, related_name='relationships')
to_user = ForeignKeyField(User, related_name='related_to')
should be changed to:
class Relationship(Model):
from_user = ForeignKeyField(User, related_name='relationships')
to_user = ForeignKeyField(User, related_name='related_to')
Keep in mind that all indentation in Python is precisely 4 spaces (of the child/nested code relative to the parent/nesting code).
Please let me know of that was the problem.
I hope that helps. Happy coding!
Peter Vann
36,427 PointsSorry, I missed a bit of the directions for (objective of) the challenge.
The Relationship class/Model should be declared OUTSIDE the User class/model
This passes:
from flask.ext.bcrypt import generate_password_hash
from flask.ext.login import UserMixin
from peewee import *
DATABASE = SqliteDatabase(':memory:')
class User(UserMixin, Model):
email = CharField(unique=True)
password = CharField(max_length=100)
join_date = DateTimeField(default=datetime.datetime.now)
bio = CharField(default='')
class Meta:
database = DATABASE
@classmethod
def new(cls, email, password):
cls.create(
email=email,
password=generate_password_hash(password)
)
class Relationship(Model):
from_user = ForeignKeyField(User, related_name='relationships')
to_user = ForeignKeyField(User, related_name='related_to')
class LunchOrder(Model):
order = TextField()
date = DateField()
user = ForeignKeyField(User, related_name="orders")
def initialize():
DATABASE.connect()
DATABASE.create_tables([User, LunchOrder], safe=True)
DATABASE.close()
Notice that there are always two blank lines between class declarations - its best-practice syntax.
I hope that helps. Happy coding!
Bonus:
Peter Vann
36,427 PointsOddly enough, you are ahead of me in that course (Build a Social Network with Flask). I just started it. What do you think of it so far?
Roneshia Allen
5,192 PointsRoneshia Allen
5,192 PointsHey Peter, thanks for reaching out. I put the code in and now its giving me the error message, "NameError: name 'User' is not defined".