What type of data should be used for image in sqlite database in bulb?

So, I have this db.model in my sqlite database in Flask. It looks like this:

class Drink(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(64), index = True)
    kind = db.Column(db.String(64), index = True)
    image = db.Column(db.LargeBinary)

    def __init__(self, name, kind, image):
        self.name = name
        self.kind = kind
        self.image = image

    def __repr__(self):
        return '<Drink %r>' % self.name

So, this problem is that I have this column, an image that will be the actual picture, but I don’t know what data type to use in the flask code.

Here is the flash code: Flask

class DrinkAPI(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('name', type = str, required = True, help = 'No name title provided', location = 'json')
        self.reqparse.add_argument('type', type = str, required = True, help='No type provided', location = 'json')
        self.reqparse.add_argument('image', type = blob, required = True, help='No image provided', location = 'json')
        super(DrinkAPI, self).__init__()

    def get(self, id):
        if checkDrink(id):
            info = getDrinkInfo(id)
            return {'id': id, 'name': info[0], 'type':info[1], 'image': info[2]}
        abort(404)

    def put(self, id):
        if checkDrink(id):
            args = self.reqparse.parse_args()
            deleteDrink(id)
            drink = Drink(args['name'], args['type'], args['image'])
            addDrink(drink)
            return {'drink' : marshal(drink, drink_fields)}, 201 
        abort(404)

    def delete(self, id):
        deleteDrink(id)
        return { 'result': True}

See where I set my type reqparsefrom imageto blob? This is not even the actual data type, but I don’t know what to put there. Do I need to subclass fields.Raw? Any ideas? Thanks


NEW APPROACH TO THE QUESTION

, . . ? , .jpg?

+4
2

parser.add_argument (, = werkzeug.datastructures.FileStorage, location = files)

+4

, , , .

Flask-Uploads, .

, :

photos = UploadSet('photos', IMAGES)

@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        filename = photos.save(request.files['photo'])
        rec = Photo(filename=filename, user=g.user.id)
        rec.store()
        flash("Photo saved.")
        return redirect(url_for('show', id=rec.id))
    return render_template('upload.html')

@app.route('/photo/<id>')
def show(id):
    photo = Photo.load(id)
    if photo is None:
        abort(404)
    url = photos.url(photo.filename)
    return render_template('show.html', url=url, photo=photo)

.save() . Flask-Uploads , . .url() .path(), .

+4

All Articles