@@ -39,6 +39,7 @@ A variation can be defined both as a tuple or a dictionary.
3939
4040Example:
4141``` python
42+ from django.db import models
4243from stdimage.models import StdImageField
4344
4445
@@ -59,7 +60,7 @@ class MyModel(models.Model):
5960 })
6061
6162 # # 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 = {
6364 ' large' : (600 , 400 ),
6465 ' thumbnail' : (100 , 100 , True ),
6566 ' medium' : (300 , 200 ),
@@ -74,36 +75,11 @@ Example:
7475```
7576
7677### 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' ))
9478
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 ] .
9781
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
10783
10884### Validators
10985The ` 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.
11288
11389 Example
11490``` python
91+ from django.db import models
11592from stdimage.validators import MinSizeValidator, MaxSizeValidator
93+ from stdimage.models import StdImageField
11694
11795
118- class MyClass (models .Model )
96+ class MyClass (models .Model ):
11997 image1 = StdImageField(validators = [MinSizeValidator(800 , 600 )])
12098 image2 = StdImageField(validators = [MaxSizeValidator(1028 , 768 )])
12199```
@@ -133,11 +111,14 @@ Clearing the field if blank is true, does not delete the file. This can also be
133111This packages contains two signal callback methods that handle file deletion for all SdtImageFields of a model.
134112
135113``` python
114+ from django.db.models.signals import pre_delete, pre_save
136115from stdimage.utils import pre_delete_delete_callback, pre_save_delete_callback
137116
117+ from . import models
138118
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)
141122```
142123
143124** Warning:** You should not use the signal callbacks in production. They may result in data loss.
156137 from django.apps import apps
157138 get_model = apps.get_model
158139except ImportError :
159- from django.db.models.loading import get_model
140+ from django.apps import apps
160141
161142from celery import shared_task
162143
@@ -166,7 +147,7 @@ from stdimage.utils import render_variations
166147@shared_task
167148def process_photo_image (file_name , variations , storage ):
168149 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)
170151 obj.processed = True
171152 obj.save()
172153```
@@ -175,18 +156,17 @@ def process_photo_image(file_name, variations, storage):
175156``` python
176157from django.db import models
177158from stdimage.models import StdImageField
178- from stdimage.utils import UploadToClassNameDir
179159
180160from tasks import process_photo_image
181161
182162def image_processor (file_name , variations , storage ):
183163 process_photo_image.delay(file_name, variations, storage)
184164 return False # prevent default rendering
185165
186- class AsyncImageModel(models.Model)
166+ class AsyncImageModel (models .Model ):
187167 image = StdImageField(
188168 # above task definition can only handle one model object per image filename
189- upload_to = UploadToClassNameDir() ,
169+ upload_to = ' path/to/file/ ' ,
190170 render_variations = image_processor # pass boolean or callable
191171 )
192172 processed = models.BooleanField(default = False ) # flag that could be used for view querysets
0 commit comments