pallets-eco / flask-security

Quick and simple security for Flask applications
MIT License
1.63k stars 513 forks source link

Basic Peewee Application import order conflict #849

Open sistemicorp opened 4 years ago

sistemicorp commented 4 years ago

When I try and run the example here I get the following traceback,

Traceback (most recent call last):
  File "/home/martin/PycharmProjects/flaskPeewee_01/test.py", line 20, in <module>
    class Role(db.Model, RoleMixin):
AttributeError: 'Database' object has no attribute 'Model'

To fix the problem, I changed the import order to,

from flask import Flask, render_template
from peewee import *
from flask_peewee.db import Database
from flask_security import Security, PeeweeUserDatastore, \
    UserMixin, RoleMixin, login_required

Versions: flask-peewee==3.0.3 peewee==3.10.0

nelsonkam commented 4 years ago

Hello!

The peewee example from the Flask-Security website is quite outdated. flask-peewee is deprecated. I suggest you try the following snippet:

from peewee import *
from playhouse.flask_utils import FlaskDB
from flask_security import Security, PeeweeUserDatastore, \
    UserMixin, RoleMixin, login_required

# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['DATABASE'] = {
    'name': 'example.db',
    'engine': 'peewee.SqliteDatabase',
}

# Create database connection object
db = FlaskDB(app) # note that I used FlaskDB instead of Database
sistemicorp commented 4 years ago

I made the above changes and my program still works. Thank you.