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 trialKhoa Nguyen
1,781 PointsWhat is purpose of class Meta ? and Why is my code wrong
Hello,
Can you explain what is class Meta and Why my code is 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(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, generate_password_hash(password))
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsWhen Python creates a class
object, special construction instructions can be provided. This is done through the Meta
class. In this case, the Model
base class includes methods for creating and saving instances of this class to a database. This requires knowing which database to use. Since the database isn't part of the class itself, this class constructor information is provided through the special Meta
class.
For a great read on the details of using class Meta
in Python, see this StackOverflow answer Classes as Objects
Your code fails because .create()
is looking for keyword argument, not positional argument:
@classmethod
def new(cls, email, password):
cls.create(email=email, password=generate_password_hash(password))