-
Notifications
You must be signed in to change notification settings - Fork 534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an example of using Django's file storage API to open files #3997
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,34 @@ For further examples which deal with files held on typical cloud services please | |
|
||
|
||
|
||
---------- | ||
|
||
|
||
Opening Django Files | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Django implements a `File Storage API <https://docs.djangoproject.com/en/5.1/ref/files/storage/>`_ to store files. The default is the `FileSystemStorage <https://docs.djangoproject.com/en/5.1/ref/files/storage/#the-filesystemstorage-class>`_, but the `django-storages <https://django-storages.readthedocs.io/en/latest/index.html>`_ library provides a number of other storage backends. | ||
|
||
You can open the file, move the contents into memory, then pass the contents to |PyMuPDF| as a stream. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about including a standout note here? Something like:
|
||
.. code-block:: python | ||
|
||
import pymupdf | ||
from django.core.files.storage import default_storage | ||
|
||
from .models import MyModel | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does "MyModel" come from - is that part of Django? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, so |
||
|
||
obj = MyModel.objects.get(id=1) | ||
with default_storage.open(obj.file.name) as f: | ||
data = f.read() | ||
|
||
doc = pymupdf.Document(stream=data) | ||
|
||
Please note that if the file you open is large, you may run out of memory. | ||
|
||
The File Storage API works well if you're using different storage backends in different environments. If you're only using the `FileSystemStorage`, you can simply use the `obj.file.name` to open the file directly with |PyMuPDF| as shown in an earlier example. | ||
|
||
|
||
---------- | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually better as: "Opening Files from the Django Storage Area" - as we are still opening PDFs etc. that might be stored there right? ( i.e. we are not opening "Django system files" )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are totally correct. I'm embarrassed to see that you saw the original title!
The convention in Django is to call these "media files" or "user-uploaded media". How do you feel about "Opening Media Files in a Django Application"?