@@ -39,6 +39,7 @@ A variation can be defined both as a tuple or a dictionary.
39
39
40
40
Example:
41
41
``` python
42
+ from django.db import models
42
43
from stdimage.models import StdImageField
43
44
44
45
@@ -59,7 +60,7 @@ class MyModel(models.Model):
59
60
})
60
61
61
62
# # Full ammo here. Please note all the definitions below are equal
62
- image = StdImageField(upload_to = upload_to , blank = True , variations = {
63
+ image = StdImageField(upload_to = ' path/to/img ' , blank = True , variations = {
63
64
' large' : (600 , 400 ),
64
65
' thumbnail' : (100 , 100 , True ),
65
66
' medium' : (300 , 200 ),
@@ -74,36 +75,11 @@ Example:
74
75
```
75
76
76
77
### Utils
77
- By default StdImageField stores images without modifying the file name.
78
- If you want to use more consistent file names you can use the build in upload callables.
79
-
80
- Example:
81
- ``` python
82
- from stdimage.utils import UploadToUUID, UploadToClassNameDir, UploadToAutoSlug, \
83
- UploadToAutoSlugClassNameDir
84
-
85
-
86
- class MyClass (models .Model ):
87
- title = models.CharField(max_length = 50 )
88
-
89
- # Gets saved to MEDIA_ROOT/myclass/#FILENAME#.#EXT#
90
- image1 = StdImageField(upload_to = UploadToClassNameDir())
91
-
92
- # Gets saved to MEDIA_ROOT/myclass/pic.#EXT#
93
- image2 = StdImageField(upload_to = UploadToClassNameDir(name = ' pic' ))
94
78
95
- # Gets saved to MEDIA_ROOT/images/#UUID#.#EXT#
96
- image3 = StdImageField( upload_to = UploadToUUID( path = ' images ' ))
79
+ Since version 4 the custom ` upload_to ` utils have been dropped in favor of
80
+ [ Django Dynamic Filenames ] [ dynamic_filenames ] .
97
81
98
- # Gets saved to MEDIA_ROOT/myclass/#UUID#.#EXT#
99
- image4 = StdImageField(upload_to = UploadToClassNameDirUUID())
100
-
101
- # Gets save to MEDIA_ROOT/images/#SLUG#.#EXT#
102
- image5 = StdImageField(upload_to = UploadToAutoSlug(populate_from = ' title' ))
103
-
104
- # Gets save to MEDIA_ROOT/myclass/#SLUG#.#EXT#
105
- image6 = StdImageField(upload_to = UploadToAutoSlugClassNameDir(populate_from = ' title' ))
106
- ```
82
+ [ dynamic_filenames ] : https://github.com/codingjoe/django-dynamic-filenames
107
83
108
84
### Validators
109
85
The ` StdImageField ` doesn't implement any size validation. Validation can be specified using the validator attribute
@@ -112,10 +88,12 @@ Validators can be used for both Forms and Models.
112
88
113
89
Example
114
90
``` python
91
+ from django.db import models
115
92
from stdimage.validators import MinSizeValidator, MaxSizeValidator
93
+ from stdimage.models import StdImageField
116
94
117
95
118
- class MyClass (models .Model )
96
+ class MyClass (models .Model ):
119
97
image1 = StdImageField(validators = [MinSizeValidator(800 , 600 )])
120
98
image2 = StdImageField(validators = [MaxSizeValidator(1028 , 768 )])
121
99
```
@@ -133,11 +111,14 @@ Clearing the field if blank is true, does not delete the file. This can also be
133
111
This packages contains two signal callback methods that handle file deletion for all SdtImageFields of a model.
134
112
135
113
``` python
114
+ from django.db.models.signals import pre_delete, pre_save
136
115
from stdimage.utils import pre_delete_delete_callback, pre_save_delete_callback
137
116
117
+ from . import models
138
118
139
- post_delete.connect(pre_delete_delete_callback, sender = MyModel)
140
- pre_save.connect(pre_save_delete_callback, sender = MyModel)
119
+
120
+ pre_delete.connect(pre_delete_delete_callback, sender = models.MyModel)
121
+ pre_save.connect(pre_save_delete_callback, sender = models.MyModel)
141
122
```
142
123
143
124
** Warning:** You should not use the signal callbacks in production. They may result in data loss.
156
137
from django.apps import apps
157
138
get_model = apps.get_model
158
139
except ImportError :
159
- from django.db.models.loading import get_model
140
+ from django.apps import apps
160
141
161
142
from celery import shared_task
162
143
@@ -166,7 +147,7 @@ from stdimage.utils import render_variations
166
147
@shared_task
167
148
def process_photo_image (file_name , variations , storage ):
168
149
render_variations(file_name, variations, replace = True , storage = storage)
169
- obj = get_model(' myapp' , ' Photo' ).objects.get(image = file_name)
150
+ obj = apps. get_model(' myapp' , ' Photo' ).objects.get(image = file_name)
170
151
obj.processed = True
171
152
obj.save()
172
153
```
@@ -175,18 +156,17 @@ def process_photo_image(file_name, variations, storage):
175
156
``` python
176
157
from django.db import models
177
158
from stdimage.models import StdImageField
178
- from stdimage.utils import UploadToClassNameDir
179
159
180
160
from tasks import process_photo_image
181
161
182
162
def image_processor (file_name , variations , storage ):
183
163
process_photo_image.delay(file_name, variations, storage)
184
164
return False # prevent default rendering
185
165
186
- class AsyncImageModel(models.Model)
166
+ class AsyncImageModel (models .Model ):
187
167
image = StdImageField(
188
168
# above task definition can only handle one model object per image filename
189
- upload_to = UploadToClassNameDir() ,
169
+ upload_to = ' path/to/file/ ' ,
190
170
render_variations = image_processor # pass boolean or callable
191
171
)
192
172
processed = models.BooleanField(default = False ) # flag that could be used for view querysets
0 commit comments