Question

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 issue is that I have this column, image, which will be an actual picture, but I don't know what datatype to use in the flask code.

Here is the flask 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 the type of my reqparse of image to blob? That's not even an actual datatype, but I don't know what to put there. Do I need to subclass the fields.Raw? Any ideas? Thanks


NEW APPROACH TO QUESTION

Based on some comments, it seems like I should be storing the image in my static folder. I can do that. But then how do I reference it with my database? Is it a string that corresponds to the .jpg file?

Was it helpful?

Solution

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

OTHER TIPS

Normally images like this also need to be uploaded from a browser in addition to being served back out.

For that reason I use Flask-Uploads which handles all of the storage and naming issues for you when uploaded and when served.

Here's an example from the docs of how to use it:

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)

You use the .save() method to store the incoming file in a directory. Flask-Uploads returns to you the filename it was saved with. Later, you use .url() or .path() to get a reference to the saved file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top