简体   繁体   English

文件数据库关系,sqlalchemy,flask

[英]File database relationship, sqlalchemy, flask

Good day, every one! 今天是个好日子!
I'm making a website using flask where teacher can post a homework file and student can comment on the post with a solution file attached. 我正在使用flask创建一个网站,老师可以在其中发布家庭作业文件,而学生可以在评论后附加解决方案文件。 But i'm stuck with establishing the relationship between homework file and solution file. 但是我坚持要建立家庭作业文件和解决方案文件之间的关系。 Here is my models file: 这是我的模型文件:

from blog import login
from blog import db
from datetime import datetime
from flask_login import UserMixin


@login.user_loader
def load_user(id):
    return User.query.get(int(id))


class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    access_level = db.Column(db.Integer)
    username = db.Column(db.String(64), index=True, unique=True)
    password = db.Column(db.String(128))
    fullname = db.Column(db.String(128), index=True)
    email = db.Column(db.String(120), index=True, unique=True)
    phone_number = db.Column(db.String(10), index=True, unique=True)
    homework = db.relationship("HomeWork", backref="author", 
        lazy="dynamic")
    answer = db.relationship("Solution", backref="author", 
        lazy="dynamic")

    def __repr__(self):
        return "<User {}>".format(self.username)

    def check_password(self, password):
        return self.password == password

    def is_teacher(self):
        return self.access_level == 1


class HomeWork(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32))
    data = db.Column(db.LargeBinary)
    timestamp = db.Column(db.DateTime, index=True, 
        default=datetime.now)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))



class Solution(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, index=True, 
        default=datetime.now)
    data = db.Column(db.LargeBinary)

I want to save homeworks file into database so student can view and then post solutions for each homework . 我想将作业文件保存到数据库中,以便学生查看并发布每个作业的解决方案。 When someone go to a homework page, it will show the homework file in main post and comments of student include their solution file for the homework 当某人转到作业页面时,它将在主帖子中显示作业文件,学生的评论包括他们的作业解决方案文件
Please help me solve this problem! 请帮我解决这个问题!
Thank you so much! 非常感谢!

I assume your problem is to save the homework file into the database so that the students can view it and then post solutions, right? 我认为您的问题是将作业文件保存到数据库中,以便学生可以查看它,然后发布解决方案,对吗? So, if that's the case, you don't directly store a file into the SQL database. 因此,如果是这样,您就不会直接将文件存储到SQL数据库中。

Instead, you upload the file in a particular directory, say, 'homework' (All the files will be uploaded here). 而是将文件上载到特定目录中,例如“ homework”(所有文件都将在此处上载)。

And while uploading, you'll rename the file with a "unique id" (this can be skipped, but it is a good practice. since the names wouldn't always be in an orderly fashion) 并且在上传时,您将使用“唯一ID”对文件进行重命名(可以跳过此操作,但这是一个好习惯。因为名称不一定总是按顺序排列)

Then store the "unique id" in the 'homework' table against its actual name and other metadata like Teacher, etc. 然后将“唯一ID”与其实际名称和其他元数据(如“老师”等)相对应,存储在“作业”表中。

So, whenever a student requests for a homework file, the "unique id" (which also happens to be the filename) against it is found. 因此,每当学生请求作业文件时,就会找到针对它的“唯一ID”(也恰好是文件名)。 And then the file with the same "unique id" is returned. 然后返回具有相同“唯一ID”的文件。

A similar way for your the 'solutions'. 一种类似的解决方案。

A good link to get you started with file uploading in Flask: Flask File Upload 一个很好的链接,可以帮助您开始使用Flask上传文件Flask File Upload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM