AttributeError: 'list' object has no attribute '_meta'
AttributeError: 'list' object has no attribute '_meta'
为什么我的代码会报错?
AttributeError: 'list' 对象没有属性 '_meta'
我的Model.py代码
import datetime
from flask.ext.login import UserMixin
from peewee import *
DATABASE = SqliteDatabase('social.db')
class User(UserMixin, Model):
username = CharField(unique=True)
email = CharField(unique=True)
password = CharField(max_length=100)
joined_at = DateTimeField(default=datetime.datetime.now)
is_admin = BooleanField(default=False)
class Meta:
database = DATABASE
order_by = ('-joined_at',)
@classmethod
def create_user(cls,user_name,email,password, admin=False):
try:
cls.create(
user_name=user_name,
email=email,
password=generate_password_hash(password),
is_admin=admin)
except IntegrityError:
raise valueError("User Already exists")
def initialize():
DATABASE.connect()
DATABASE.create_table([User],safe=True)
DATABASE.close()
我的主要代码在app.py
from flask import (Flask, g,render_template, flash, redirect, url_for)
from flask.ext.login import LoginManager
import forms
import models
DEBUG =True
PORT=8000
HOST = '127.0.0.1'
app = Flask(__name__)
app.secret_key = 'abcd.1234.xyz'
login_manager =LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(userid):
try:
return models.User.get(models.User.id==userid)
except models.DoesNotExist:
return None
@app.before_request
def before_request():
"""Connect to the database before each request."""
g,db = models.DATABASE
g.db.connect()
@app.after_request
def after_request(response):
"""Connect to the database after each request."""
g.db.close()
return response
@app.route('/register', methods=('GET', 'POST'))
def register():
form = forms.RegisterForm()
if form.validate_on_submit():
flask("Yay, you registered", "success")
models.User.create_user(
username=form.username.data,
email=form.email.data,
password=form.password.data
)
return redirect(url_for('index'))
return render_template('register.html', form=form)
@app.route('/index')
def index():
return 'Hey'
if __name__ == '__main__':
models.initialize()
try:
models.User.create_user(
username='shuvanon',
email='razik666@gmail.com',
password='123456',
admin=True
)
except ValueError:
pass
app.run(debug=DEBUG, host=HOST,port=PORT)
还有我的错误
Traceback (most recent call last):
File "app.py", line 59, in <module>
models.initialize()
File "F:\Python Work\Groupe\models.py", line 31, in initialize
DATABASE.create_table([User],safe=True)
File "D:\Program Files\Python27\lib\site-packages\peewee.py", line 3540, in create_table
return self.execute_sql(*qc.create_table(model_class, safe))
File "D:\Program Files\Python27\lib\site-packages\peewee.py", line 1929, in inner
return self.parse_node(fn(*args, **kwargs))
File "D:\Program Files\Python27\lib\site-packages\peewee.py", line 1948, in _create_table
meta = model_class._meta
AttributeError: 'list' object has no attribute '_meta'
model.py文件编译正确,social.db文件也被创建。我是 flask 和 peewee 的新手,找不到问题。
您传入的是一个列表,不是单个模型:
DATABASE.create_table([User], safe=True)
Database.create_table()
method只取一个模型;以下将起作用:
DATABASE.create_table(User, safe=True)
您也可以使用 Database.create_tables()
method(注意末尾额外的 s
),这确实需要一系列模型:
DATABASE.create_tables([User], safe=True)
请注意,Database.create_table()
方法(没有尾随 s
)仅创建一个 table 本身,而不是任何依赖项,例如索引或约束。我怀疑您只是打错了字,忘记了 s
.
为什么我的代码会报错?
AttributeError: 'list' 对象没有属性 '_meta'
我的Model.py代码
import datetime
from flask.ext.login import UserMixin
from peewee import *
DATABASE = SqliteDatabase('social.db')
class User(UserMixin, Model):
username = CharField(unique=True)
email = CharField(unique=True)
password = CharField(max_length=100)
joined_at = DateTimeField(default=datetime.datetime.now)
is_admin = BooleanField(default=False)
class Meta:
database = DATABASE
order_by = ('-joined_at',)
@classmethod
def create_user(cls,user_name,email,password, admin=False):
try:
cls.create(
user_name=user_name,
email=email,
password=generate_password_hash(password),
is_admin=admin)
except IntegrityError:
raise valueError("User Already exists")
def initialize():
DATABASE.connect()
DATABASE.create_table([User],safe=True)
DATABASE.close()
我的主要代码在app.py
from flask import (Flask, g,render_template, flash, redirect, url_for)
from flask.ext.login import LoginManager
import forms
import models
DEBUG =True
PORT=8000
HOST = '127.0.0.1'
app = Flask(__name__)
app.secret_key = 'abcd.1234.xyz'
login_manager =LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(userid):
try:
return models.User.get(models.User.id==userid)
except models.DoesNotExist:
return None
@app.before_request
def before_request():
"""Connect to the database before each request."""
g,db = models.DATABASE
g.db.connect()
@app.after_request
def after_request(response):
"""Connect to the database after each request."""
g.db.close()
return response
@app.route('/register', methods=('GET', 'POST'))
def register():
form = forms.RegisterForm()
if form.validate_on_submit():
flask("Yay, you registered", "success")
models.User.create_user(
username=form.username.data,
email=form.email.data,
password=form.password.data
)
return redirect(url_for('index'))
return render_template('register.html', form=form)
@app.route('/index')
def index():
return 'Hey'
if __name__ == '__main__':
models.initialize()
try:
models.User.create_user(
username='shuvanon',
email='razik666@gmail.com',
password='123456',
admin=True
)
except ValueError:
pass
app.run(debug=DEBUG, host=HOST,port=PORT)
还有我的错误
Traceback (most recent call last):
File "app.py", line 59, in <module>
models.initialize()
File "F:\Python Work\Groupe\models.py", line 31, in initialize
DATABASE.create_table([User],safe=True)
File "D:\Program Files\Python27\lib\site-packages\peewee.py", line 3540, in create_table
return self.execute_sql(*qc.create_table(model_class, safe))
File "D:\Program Files\Python27\lib\site-packages\peewee.py", line 1929, in inner
return self.parse_node(fn(*args, **kwargs))
File "D:\Program Files\Python27\lib\site-packages\peewee.py", line 1948, in _create_table
meta = model_class._meta
AttributeError: 'list' object has no attribute '_meta'
model.py文件编译正确,social.db文件也被创建。我是 flask 和 peewee 的新手,找不到问题。
您传入的是一个列表,不是单个模型:
DATABASE.create_table([User], safe=True)
Database.create_table()
method只取一个模型;以下将起作用:
DATABASE.create_table(User, safe=True)
您也可以使用 Database.create_tables()
method(注意末尾额外的 s
),这确实需要一系列模型:
DATABASE.create_tables([User], safe=True)
请注意,Database.create_table()
方法(没有尾随 s
)仅创建一个 table 本身,而不是任何依赖项,例如索引或约束。我怀疑您只是打错了字,忘记了 s
.