-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved docs. Add section
Upload File
into **Tutorial / Using file…
…s in models**
- Loading branch information
Showing
10 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
52 changes: 52 additions & 0 deletions
52
docs_src/tutorial/using-files-in-models/008_file_information.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
|
||
from libcloud.storage.drivers.local import LocalStorageDriver | ||
from sqlalchemy import Column, Integer, String, create_engine, select | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy_file import File, FileField | ||
from sqlalchemy_file.storage import StorageManager | ||
|
||
Base = declarative_base() | ||
|
||
|
||
# Define your model | ||
class Attachment(Base): | ||
__tablename__ = "attachment" | ||
|
||
id = Column(Integer, autoincrement=True, primary_key=True) | ||
name = Column(String(50), unique=True) | ||
content = Column(FileField) | ||
|
||
|
||
# Configure Storage | ||
os.makedirs("/tmp/storage/attachment", 0o777, exist_ok=True) | ||
container = LocalStorageDriver("/tmp/storage").get_container("attachment") | ||
StorageManager.add_storage("default", container) | ||
|
||
# Save your model | ||
engine = create_engine( | ||
"sqlite:///example.db", connect_args={"check_same_thread": False} | ||
) | ||
Base.metadata.create_all(engine) | ||
|
||
with Session(engine) as session: | ||
session.add(Attachment(name="attachment1", content=open("./example.txt", "rb"))) | ||
session.add(Attachment(name="attachment2", content=b"Hello world")) | ||
session.add(Attachment(name="attachment3", content="Hello world")) | ||
file = File(content="Hello World", filename="hello.txt", content_type="text/plain") | ||
session.add(Attachment(name="attachment4", content=file)) | ||
session.commit() | ||
|
||
attachment = session.execute( | ||
select(Attachment).where(Attachment.name == "attachment3") | ||
).scalar_one() | ||
assert attachment.content.saved # saved is True for saved file | ||
assert attachment.content.file.read() == b"Hello world" # access file content | ||
assert ( | ||
attachment.content["filename"] is not None | ||
) # `unnamed` when no filename are provided | ||
assert attachment.content["file_id"] is not None # uuid v4 | ||
assert attachment.content["upload_storage"] == "default" | ||
assert attachment.content["content_type"] is not None | ||
assert attachment.content["uploaded_at"] is not None |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters