重寫Django 模型的選擇性調整大小的保存方法
問題:
問題:問題:
問題:
問題:<code class="python">class Model(model.Model): _image = models.ImageField(upload_to='folder') thumb = models.ImageField(upload_to='folder') description = models.CharField() def set_image(self, val): self._image = val self._image_changed = True # Or put whole logic in here small = rescale_image(self.image, width=100, height=100) self.image_small = SimpleUploadedFile(name, small_pic) def get_image(self): return self._image image = property(get_image, set_image) def save(self, *args, **kwargs): if getattr(self, '_image_changed', True): small = rescale_image(self.image, width=100, height=100) self.image_small = SimpleUploadedFile(name, small_pic) super(Model, self).save(*args, **kwargs)</code>
以上是如何重寫 Django 模型的保存方法以選擇性調整圖像大小?的詳細內容。更多資訊請關注PHP中文網其他相關文章!