https://github.com/jameelhamdan/django-gridfs-storage
Pluggable Django GridFS storage.
https://github.com/jameelhamdan/django-gridfs-storage
django gridfs gridfs-strorage mongodb pymongo
Last synced: 5 months ago
JSON representation
Pluggable Django GridFS storage.
- Host: GitHub
- URL: https://github.com/jameelhamdan/django-gridfs-storage
- Owner: jameelhamdan
- License: mit
- Created: 2020-08-27T13:39:19.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-28T09:49:49.000Z (almost 6 years ago)
- Last Synced: 2025-04-07T11:47:27.141Z (about 1 year ago)
- Topics: django, gridfs, gridfs-strorage, mongodb, pymongo
- Language: Python
- Homepage:
- Size: 24.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# django-gridfs-storage
### Simple django GridFS storage engine
## Usage:
-
Install django_gridfs_storage:
```
pip install django_gridfs_storage
```
-
Into settings.py file of your project, add gridfs_storage to INSTALLED_APPS:
```python
INSTALLED_APPS = [
...,
'gridfs_storage',
]
```
-
add the following variables to your settings:
```python
# defaults to default local mongodb server
DEFAULT_GRIDFS_URL = 'mongodb://127.0.0.1:27017'
# if set to None, it will refuse to serve files and raise an Exception
DEFAULT_GRIDFS_SERVE_URL = None
DEFAULT_GRIDFS_COLLECTION = 'storage'
```
-
To serve files through django (not recommended) you can use this in urls.py:
```python
urlpatterns = [
path('admin/', admin.site.urls),
...,
path('media/', include('gridfs_storage.urls')),
]
```
and set the DEFAULT_GRIDFS_SERVE_URL to the prefix you specified in the path. in this case its /media/
-
If you wish to use it on all FileField and ImageField set it as the default Storage:
```python
DEFAULT_FILE_STORAGE = 'gridfs_storage.storage.GridFSStorage'
```
-
If you wish to use on individual field bases set it as the field storage:
```python
from django.db import models
from gridfs_storage.storage import GridFSStorage
class SampleModel(models.Model):
attachment = models.FileField(storage=GridFSStorage())
first_pic = models.ImageField(storage=GridFSStorage(location='sample/images'))
# To store in a different collection than "storage"
another_pic = models.ImageField(storage=GridFSStorage(collection='image_storage'))
# Serve through custom cdn connected to the same gridfs or similar, the limit is the sky :)
served_outside = models.ImageField(storage=GridFSStorage(base_url='https://img.cdn/serve/'))
```
## Requirements:
1. Python 3.6 or higher.
2. Django 2.2 or higher.
3. MongoDB 3.4 or higher.
## Tests? None.
We crash production like real men