Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to declare an image field, sqlalchemy

I am trying to build and app with flask, I have seen some tutorial and books and some code[1] they explain how to declare integers and string for the database setup. However, they do not explain how to store images. I am very confused now I though the natural way to store images was on a database, but reading some documentation on the flask site[2] the filesystem is the place to store images. I just make a first assumption reading a flask development book that a declaration with largeBinary would be the correct one, but this is just my guess as you can see in the code below. can somebody help me to figure this out? if what I ask is not clear please let me know.

Class User(Base):
    __table__ = 'user'

    id = Column(Integer, primary_key = True)
    name  Column(String(80), nullable=False)
    email =  Column(String(250), nullable=False)
    image = Column(LargeBinary, nullable = True)

[1]https://github.com/lobrown/Full-Stack-Foundations/blob/master/Lesson_1/database_setup.py [2]http://flask.pocoo.org/docs/0.10/patterns/fileuploads/

like image 869
Jass Avatar asked Dec 08 '15 11:12

Jass


2 Answers

I read in this post[1] that the best way is to declare the image field as a string to hold an url or link to the image, the image then can be stored in a external server, different from the server of your application. So if somebody have the same doubt just declare it as string and continue with your app development. A little disappointed of database management systems they only can be used to store numerical and alphabetical data(:p).

[1]https://www.reddit.com/r/flask/comments/31tywp/afbest_way_to_store_avatars_images_etc/

like image 121
Jass Avatar answered Nov 08 '22 10:11

Jass


There is a library called SQLAlchemy-ImageAttach which provides a way to create entities having an image object. These could be exposed via some url.

Otherwise you can get a file object straight-forward via the SingleImageSet class.

With the lib you can choose from two of storages, such as the filesystem's or Amazon's S3. Inside the docs there is a described example (grey box) how to define previously the storage and then an User entity and owning a UserPicture entity.

from sqlalchemy import Column, ForeignKey, Integer, Unicode
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_imageattach.entity import Image, image_attachment

Base = declarative_base()

class User(Base):
"""User model."""

    id = Column(Integer, primary_key=True)
    name = Column(Unicode, nullable=False)
    picture = image_attachment('UserPicture')
    __tablename__ = 'user'

class UserPicture(Base, Image):
    """User picture model."""

    user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)
    user = relationship('User')
    __tablename__ = 'user_picture'

I don't know Flask, but perhaps you can use a filestorage via some wsgi declaration together with the Lib here. I hope it helps you a bit and gives you some **.

like image 40
Semo Avatar answered Nov 08 '22 09:11

Semo