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


Sam Dale
Sam Dale
6,136 Points

Model Initialization Error

Hi there. I definitely struggled in learning Flask, so there's probably a simple solution to my problem, but for my tacocat code, I'm getting the error

File "tacocat.py", line 96, in <module>
  File "/Tacocat/models.py", line 49, in initialize
    DATABASE.create_tables([User, Taco], safe = True)
(Peewee errors)
AttributeError: 'function' object has no attribute '_meta'


import datetime

from flask.ext.bcrypt import generate_password_hash
from flask.ext.login import UserMixin
from peewee import *

DATABASE = SqliteDatabase("tacocat.db")

class User(UserMixin, Model):
    email = CharField(unique = True)
    password = CharField(max_length = 100)

    class Meta:
        database = DATABASE

    def create_user(cls, email, password):
            with DATABASE.transaction():
                    email = email,
                    password = generate_password_hash(password)
        except IntegrityError:
            raise ValueError("User already exists.")

def Taco(Model):
    protein = CharField()
    shell = CharField()
    cheese = BooleanField()
    extras = TextField()

    timestamp = DateTimeField(default = datetime.datetime.now)
    user = ForeignKeyField(
        rel_model = User,
        related_name = "tacos"

    class Meta:
        database = DATABASE

def initialize():
    DATABASE.create_tables([User, Taco], safe = True)

and my rough draft of tacocat.py:

from flask import (Flask, g, render_template, flash, redirect, url_for,
from flask.ext.bcrypt import check_password_hash
from flask.ext.login import (LoginManager, login_user, logout_user,
                             login_required, current_user)

import forms
import models

DEBUG = True
PORT = 8000
HOST = ""

app = Flask(__name__)
app.secret_key = "asdfkaljf3j2-3$ADGa@#5AF#$TJ(KJ(L:$%:JA))"

login_manager = LoginManager()
login_manager.login_view = "login"

def load_user(userid):
        return models.User.get(models.User.id == userid)
    except models.DoesNotExist:
        return None

def before_request():
    """Connect to the database before each request."""
    g.db = models.DATABASE
    g.user = current_user

def after_request(response):
    """Close the database connection after each request."""
    return response

@app.route("/register", methods = ("GET", "POST"))
def register():
    form = forms.RegisterForm()
    if form.validate_on_submit():
        flash("Yay, you registered!", "success")
            email = form.email.data,
            password = form.password.data
        return redirect(url_for("index"))
    return render_template("register.html", form = form)

@app.route("/login", methods=("GET", "POST"))
def login():
    form = forms.LoginForm()
    if form.validate_on_submit():
            user = models.User.get(models.User.email == form.email.data)
        except models.DoesNotExist:
            flash("Your email or password doesn't match!", "error")
            if check_password_hash(user.password, form.password.data):
                flash("You've been logged in!", "success")
                return redirect(url_for("index"))
                flash("Your email or password doesn't match!", "error")

    return render_template("login.html", form=form)

def logout():
    flash("You've been logged out! Come back soon!", "success")
    return redirect(url_for("index"))

def index():
    stream = models.Post.select().limit(100)
    return render_template("stream.html", stream = stream)

def not_found(error):
    return render_template("404.html"), 404

if __name__ == "__main__":
            email = "sam@example.com",
            password = "password"
    except ValueError:
    app.run(debug = DEBUG, host = HOST, port = PORT)


1 Answer

Nathan Tallack
Nathan Tallack
22,159 Points

Again, close.

You want to change def to class for your Taco model. ;)

Let me know the next bug you hit. :)

Sam Dale
Sam Dale
6,136 Points

Oh my god. Thank you. I think this is a sign that I probably should have gone to sleep earlier.