我有以下型号:
# Define the User data model. Make sure to add the flask_user.UserMixin !!
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
# User email information (required for Flask-User)
email = db.Column(db.String(255), nullable=False, unique=True)
confirmed_at = db.Column(db.DateTime())
# User information
active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0')
first_name = db.Column(db.String(50), nullable=False, server_default='')
last_name = db.Column(db.String(50), nullable=False, server_default='')
clinic_name = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_one = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_city = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_state = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_zip = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_phone = db.Column(db.String(50), nullable=False, server_default='')
# Relationships
user_auth = db.relationship('UserAuth', uselist=False)
roles = db.relationship('Role', secondary='user_roles',
backref=db.backref('users', lazy='dynamic'))
memorials = db.relationship('Memorial', backref='author', lazy='dynamic')
# Define the UserAuth data model.
class UserAuth(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
# User authentication information (required for Flask-User)
username = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')
reset_password_token = db.Column(db.String(100), nullable=False, server_default='')
active = db.Column(db.Boolean(), nullable=False, server_default='0')
# Relationships
user = db.relationship('User', uselist=False)
# Define the Role data model
class Role(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(50), unique=True)
description = db.Column(db.String(255))
# Define the UserRoles association model
class UserRoles(db.Model):
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('role.id', ondelete='CASCADE'))
# Define the Memorial model
class Memorial(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
owner_name = db.Column(db.String(255), nullable=False, server_default='')
owner_address_one = db.Column(db.String(255), nullable=False, server_default='')
owner_address_two = db.Column(db.String(255), nullable=False, server_default='')
owner_city = db.Column(db.String(255), nullable=False, server_default='')
owner_state = db.Column(db.String(255), nullable=False, server_default='')
owner_zipcode = db.Column(db.String(255), nullable=False, server_default='')
pet_name = db.Column(db.String(255), nullable=False, server_default='')
pet_gender = db.Column(db.String(255), nullable=False, server_default='')
pet_species = db.Column(db.String(255), nullable=False, server_default='')
我有一个表格可以输入你的纪念品,它有这个信息:
@app.route('/member', methods=['GET','POST'])
@login_required # Limits access to authenticated users
def member_page():
# Intitialize form
form = MemorialsForm(request.form)
# Process valid POST
if request.method=='POST' and form.validate():
mems = Memorial()
form.populate_obj(mems)
db.session.add(mems)
# Save the submission
db.session.commit()
# Redirect to list page
return redirect(url_for('memorial_list'))
# Process GET or invalid POST
return render_template('flask_user/memorial.html', form=form)
对于我的列表页面,我想显示特定用户输入的所有纪念馆。我无法通过这项工作来挽救我的生命。
我试过了:
all_mems = Memorial.query.filter_by(author=current_user).all()
return render_template('pages/list.html', all_mems=all_mems)
这让我没有结果。如果我尝试做类似的事情:
all_mems = Memorial.query.filter_by(author=current_user.id).all()
它抱怨说:
AttributeError: 'int' object has no attribute '_sa_instance_state'
如果我转储数据库,除了Memorial.user_id永远不会获得赋值的事实之外,我看不到有用的东西。根据模型设置,我认为哪种自动化?
任何想法/帮助表示赞赏。如果我没有提供足够的信息,请告诉我。
++++ +++ EDIT 我现在有:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
# User email information (required for Flask-User)
email = db.Column(db.String(255), nullable=False, unique=True)
confirmed_at = db.Column(db.DateTime())
# User information
active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0')
first_name = db.Column(db.String(50), nullable=False, server_default='')
last_name = db.Column(db.String(50), nullable=False, server_default='')
clinic_name = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_one = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_city = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_state = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_zip = db.Column(db.String(50), nullable=False, server_default='')
clinic_add_phone = db.Column(db.String(50), nullable=False, server_default='')
# Relationships
memorials = db.relationship('Memorial', backref='users', lazy='dynamic') #added lazy code, changed backref to user from clinic
user_auth = db.relationship('UserAuth', uselist=False)
roles = db.relationship('Role', secondary='user_roles',
backref=db.backref('users', lazy='dynamic'))
和
class Memorial(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
owner_name = db.Column(db.String(255), nullable=False, server_default='')
owner_address_one = db.Column(db.String(255), nullable=False, server_default='')
owner_address_two = db.Column(db.String(255), nullable=False, server_default='')
owner_city = db.Column(db.String(255), nullable=False, server_default='')
owner_state = db.Column(db.String(255), nullable=False, server_default='')
owner_zipcode = db.Column(db.String(255), nullable=False, server_default='')
pet_name = db.Column(db.String(255), nullable=False, server_default='')
pet_gender = db.Column(db.String(255), nullable=False, server_default='')
pet_species = db.Column(db.String(255), nullable=False, server_default='')
status = db.Column(db.String(255), nullable=False, server_default='')
remit_date = db.Column(db.DateTime)
processed_date = db.Column(db.DateTime)
我现在可以保存,删除和查看纪念品,附加了用户ID(因为我现在正在保存user_id ...认为它是自动的,但事实并非如此)。我仍然不知道如何让我的Join很重要 - 我可以很容易地查询任何一个表,但我无法弄清楚如何获取连接数据。例如,假设我想要转储纪念品,并添加输入它的用户的clinic_name。
all_mems = Memorials.query.all()仅返回所有纪念馆和user_ids。返回所有纪念馆和诊所名称的正确构造是什么?
谢谢你们 - 我真的很开心。