声明图像字段的正确方法,sqlalchemy [英] Correct way to declare an image field, sqlalchemy

查看:124
本文介绍了声明图像字段的正确方法,sqlalchemy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用flask创建和应用程序,我看过一些教程和书籍以及一些代码[1],它们解释了如何为数据库设置声明整数和字符串.但是,它们没有解释如何存储图像.我现在很困惑,尽管存储图像的自然方法是在数据库中,但是在flask网站上阅读一些文档[2],文件系统是存储图像的地方.我只是在阅读一瓶烧瓶开发书时做出第一个假设,即使用largeBinary声明是正确的,但这只是我的猜测,如您在下面的代码中所见.有人可以帮我解决这个问题吗?如果我的要求不清楚,请告诉我.

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 /

[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/

推荐答案

有一个名为

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.

否则,您可以通过SingleImageSet类直接获取文件对象.

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

有了lib,您可以从两个存储中进行选择,例如文件系统或Amazon的S3.在文档内部有一个所描述的示例(灰色框)如何先定义存储,然后再定义一个User实体并拥有一个UserPicture实体.

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'

我不了解Flask,但是也许您可以通过一些wsgi声明和此处的Lib一起使用文件存储.希望它能对您有所帮助,并给您一些**.

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 **.

这篇关于声明图像字段的正确方法,sqlalchemy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆