Skip to content

Commit

Permalink
Merge pull request #669 from albertyw/django-storages
Browse files Browse the repository at this point in the history
Fix deprecation warning for get_storage_class
  • Loading branch information
albertyw authored Dec 30, 2023
2 parents f24f055 + b24978d commit 23ff43b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ When enabled, a graph visualisation generated using [gprof2dot](https://github.c
A custom storage class can be used for the saved generated binary `.prof` files:

```python
# For Django >= 4.2 and Django-Silk >= 5.1.0:
# See https://docs.djangoproject.com/en/5.0/ref/settings/#std-setting-STORAGES
STORAGES = {
'SILKY_STORAGE': {
'BACKEND': 'path.to.StorageClass',
},
# ...
}

# For Django < 4.2 or Django-Silk < 5.1.0
SILKY_STORAGE_CLASS = 'path.to.StorageClass'
```

Expand Down
17 changes: 15 additions & 2 deletions silk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uuid import uuid4

import sqlparse
from django.core.files.storage import get_storage_class
from django.conf import settings
from django.db import models, transaction
from django.db.models import (
BooleanField,
Expand All @@ -25,7 +25,20 @@
from silk.config import SilkyConfig
from silk.utils.profile_parser import parse_profile

silk_storage = get_storage_class(SilkyConfig().SILKY_STORAGE_CLASS)()
try:
# New in Django 4.2
from django.core.files.storage import storages
from django.core.files.storage.handler import InvalidStorageError
try:
silk_storage = storages['SILKY_STORAGE']
except InvalidStorageError:
from django.utils.module_loading import import_string
storage_class = SilkyConfig().SILKY_STORAGE_CLASS or settings.DEFAULT_FILE_STORAGE
silk_storage = import_string(storage_class)()
except ImportError:
# Deprecated since Django 4.2, Removed in Django 5.1
from django.core.files.storage import get_storage_class
silk_storage = get_storage_class(SilkyConfig().SILKY_STORAGE_CLASS)()


# Seperated out so can use in tests w/o models
Expand Down

0 comments on commit 23ff43b

Please sign in to comment.