覚書として、合計する、プロジェクトを完了するために、今日、長い時間にブログを更新し、本当に、本当にあまりにも投げ、背景を書きたくない:幸せ:、このプロジェクトは、全体はまだ非常に充実して、以前に採用フレームワークは、すぐに書いたりします。進捗状況も良好で、今日の5日間、会ったが、nは多くのピットが、彼は、疲れて行われました。
全体のフレームワーク
認証Cookieを継承別の機会に使用することができますしながら、プロジェクトの全体的な枠組みや認証の使用上のプロジェクトの青写真フレームワークは、JWTを使用しています。例えば、小さなマイクロチャネル・プログラムは、JWT認証に適しています。最近、小さなプログラムで一緒に彼のチームメイト、私はこのプロジェクトを行うには、一時的なフロントエンドの時間ので、それは漁獲量の進展を見て、今多くの時間を遅らせ、非常に良い感じ。
段付ピット概要
sa_instance_stateエラーを解決します
AttributeError: 'Manage' object has no attribute '_sa_instance_state'
このエラーは、ユーザー/コメントルーティング内部で発生し、長い時間のように、高齢者を尋ね、そして最終的に原因を見つけるだけでなく、以下のコードをデバッグする暴力の問題を解決するために失敗しています。
class Message(db.Model): __tablename__ = 'message' __table_args__ = {'mysql_charset': 'utf8'} id = db.Column(db.Integer,primary_key=True,autoincrement=True) message_content = db.Column(db.Text,nullable=False) message_from_name = db.Column(db.String(30),nullable=False) message_data = db.Column(db.DateTime,default = datetime.datetime.utcnow()) message_user_id = db.Column(db.Integer , db.ForeignKey('user.id')) def __init__(self , *args, **kwargs): super().__init__() if 'message_content' in kwargs: self.message_content = kwargs['message_content'] if 'message_from_name' in kwargs: self.message_from_name = kwargs['message_from_name'] if 'message_user_full' in kwargs: self.user = kwargs['message_user_full'] def get_message(self): dic={'message_content':self.message_content ,'message_from_name':self.message_from_name ,'message_data':self.message_data} return dic def to_json(self): return {'message_content':self.message_content,'message_from_name':self.message_from_name}
#views.py_v1部分代码
@main.route("/course/comment",methods=['POST','GET'])
@jwt_required()
def course_comment():
user = User.query.filter_by(id=current_identity.id).first()
dic = json_loads()
if 'comment_body' in dic and 'comment_course_id' in dic:
course = Course.query.filter_by(id=dic['comment_course_id']).first()
c = {}
t = 0
for i in course.comment.order_by('-comment_date').all():
c.update(
{t: {'comment_user_name': i.user.name, 'comment_body': i.comment_body, 'comment_date': i.comment_date}})
t += 1
if 'is_comment_on_user' in dic:
if bool(dic['is_comment_on_user']) and 'comment_on_user_id' in dic:
user_tmp=User.query.filter_by(id=dic['comment_on_user_id']).first()
str1 = user.name+"评论了您在"+course.course_name+"课程的评论"
mcc_print(str1)
user_tmp.message.append(Message(message_content=str1,message_from_name=user.name,message_user_full=user))
c_tmp=Comment(**dic)
user.comment.append(c_tmp)
c.update(
{t: {'comment_user_name': user.name, 'comment_body': dic['comment_body'],
'comment_date': c_tmp.comment_date}})
db.session.add(user_tmp)
db.session.commit()
else:
user.comment.append(Comment(**dic))
else:
user.comment.append(Comment(**dic))
db.session.add(user)
db.session.commit()
return jsonify({'StatusCode': 200, 'info': '评论用户成功', 'comment_all': c})
else:
return jsonify({'StatusCode':400,'info':'请求未填写完整'})
#views.py_v2部分代码
@main.route("/course/comment",methods=['POST','GET'])
@jwt_required()
def course_comment():
user = User.query.filter_by(id=current_identity.id).first()
dic = json_loads()
if 'comment_body' in dic and 'comment_course_id' in dic:
course = Course.query.filter_by(id=dic['comment_course_id']).first()
大专栏 AXX项目总结c = {}
t = 0
for i in course.comment.order_by('-comment_date').all():
c.update(
{t: {'comment_user_name': i.user.name, 'comment_body': i.comment_body, 'comment_date': i.comment_date}})
t += 1
if 'is_comment_on_user' in dic:
if bool(dic['is_comment_on_user']) and 'comment_on_user_id' in dic:
user_tmp=User.query.filter_by(id=dic['comment_on_user_id']).first()
str1 = user.name+"评论了您在"+course.course_name+"课程的评论"
mcc_print(str1)
message=Message()
message.message_content=str1
message.message_from_name=user.name
message.user=user_tmp
c_tmp=Comment(**dic)
user.comment.append(c_tmp)
c.update(
{t: {'comment_user_name': user.name, 'comment_body': dic['comment_body'],
'comment_date': c_tmp.comment_date}})
db.session.add(user_tmp)
db.session.commit()
else:
user.comment.append(Comment(**dic))
else:
user.comment.append(Comment(**dic))
db.session.add(user)
db.session.commit()
return jsonify({'StatusCode': 200, 'info': '评论用户成功', 'comment_all': c})
else:
return jsonify({'StatusCode':400,'info':'请求未填写完整'})
非常に明確な、しかし最終的には解決していない複数の理由のために
- db.sesseion.commitエラー(とき)
#具体的报错信息因为忘记保存就不贴了,但遇到这中文题一定要检查数据库的数据要求与你提交的数据库的要求之间进行对比,比如说nullable=False的列就不能缺,否者会commit提交不了,已经数据的类型也要注意,前后端交互,通过表单传递的一般是string,因此检查前端传来的参数十分必要
- db.sesseion.add()エラー
#当时报这个错真的有点崩,完全没遇到过,要说传参出错,那也是应该在commit那一步报错,但add这一步就报错,很奇怪,最后解决方法,在query.filter_by(search_information=kwargs['search_information'])后加上first(),虽然说知道这个用法,但是,哎,可能还是老了。
def db_user_push(**kwargs):
if 'name' in kwargs:
user = User.query.filter_by(name=kwargs['name']).first()
if current_user.is_authenticated:
user = current_user._get_current_object()
if current_identity:
user = User.query.filter_by(id=current_identity.id).first()
else:
user=User(**kwargs)
if 'name' in kwargs:
user.name=kwargs['name']
if 'password' in kwargs:
user.password=generate_password_hash(kwargs['password'])
if 'confirmed' in kwargs:
user.confirmed = bool(kwargs['confirmed'])
if 'email' in kwargs:
user.email = kwargs['email']
if 'icon' in kwargs:
user.icon=kwargs['icon']
if 'search_information' in kwargs:
tmp=Search_information.query.filter_by(search_information=kwargs['search_information']).first()
if tmp:
tmp.search_time+=1
if tmp not in user.search_information:
user.search_information.append(Search_information(**kwargs))
else:
user.search_information.append(Search_information(**kwargs))
if 'comment_body' in kwargs and 'comment_course_id' in kwargs and 'comment_on_user_id' in kwargs:
user.comment.append(Comment(**kwargs))
if 'message_content' in kwargs and 'message_from_user_name' in kwargs:
user.message.append(Message(message_content=kwargs['message_content'],message_from_name=kwargs['message_from_name']))
if 'role' in kwargs:
user.Role = Role.query.filter_by(role=kwargs['role']).first()
if 'course_name' in kwargs and 'course_name' in kwargs and 'course_score' in kwargs and 'course_target' in kwargs
and 'course_address' in kwargs and 'course_class_num' in kwargs and 'course_time_start' in kwargs
and 'course_time_end' in kwargs and 'course_attr' in kwargs and 'course_teacher_name' in kwargs
and 'course_check_type' in kwargs and 'course_time_week' in kwargs:
user.course.append(Course(**kwargs))
db.session.add(user)
db.session.commit()
return user
プロジェクト経験
プロジェクトはまだ深い五日はでこっそりと感じ、私は出会い、程度の意味を理解するために選択した、彼らの選択という、非常に満腹感を感じるだけでなく、彼らの次の方向、多くの時間を決定しますすべては何もありません、誰も存在しませんどのような圧力、交差フルの後、それは良いメモリになります