|
1 | 1 | # PyMODM-ImageFileField
|
2 | 2 | ImageFileField for PyMODM
|
| 3 | + |
| 4 | +``` |
| 5 | +from os.path import join |
| 6 | +
|
| 7 | +from pymodm import MongoModel, fields |
| 8 | +from imagefilefield.fields import ImageFileField |
| 9 | +
|
| 10 | +class TestImage(MongoModel): |
| 11 | + IMAGE_SIZES = { |
| 12 | + "small": {"size": (200, 200)}, |
| 13 | + "medium": {"size": (625, 625)}, |
| 14 | + "default": {"size": (800, 800), "smartcrop": True} |
| 15 | + # if key `default`, other sizes will based this size |
| 16 | + # `smartcrop` will use image detection on crop |
| 17 | + } |
| 18 | +
|
| 19 | + text = fields.CharField() |
| 20 | + image = ImageFileField( |
| 21 | + upload_to=join(MEDIA_ROOT, "test_image/%Y/%m/%d/"), |
| 22 | + blank=True, |
| 23 | + media_url=join(MEDIA_URL, "test_image/%Y/%m/%d/")) |
| 24 | + # using custom size |
| 25 | + image2 = ImageFileField( |
| 26 | + upload_to=join(MEDIA_ROOT, "test_image/%Y/%m/%d/"), |
| 27 | + blank=True, |
| 28 | + image_sizes = { |
| 29 | + "small": {"size": (200, 100), "smartcrop": True}, |
| 30 | + "medium": {"size": (525, 625), "smartcrop": True}, |
| 31 | + "big": {"size": (700, 700), "smartcrop": True} |
| 32 | + } |
| 33 | + media_url=join(MEDIA_URL, "test_image/%Y/%m/%d/")) |
| 34 | + created = fields.DateTimeField(default=timezone.now) |
| 35 | +
|
| 36 | +``` |
0 commit comments