看看来自 django-filer 的图像模型:
https ://github.com/stefanfoulis/django-filer/blob/develop/filer/models/imagemodels.py
您可以在 save 方法中执行类似的操作。
def save(self, *args, **kwargs):
if self.date_taken is None:
try:
exif_date = self.exif.get('DateTimeOriginal', None)
if exif_date is not None:
d, t = str.split(exif_date.values)
year, month, day = d.split(':')
hour, minute, second = t.split(':')
self.date_taken = datetime(
int(year), int(month), int(day),
int(hour), int(minute), int(second))
except:
pass
if self.date_taken is None:
self.date_taken = datetime.now()
self.has_all_mandatory_data = self._check_validity()
try:
# do this more efficient somehow?
self.file.seek(0)
self._width, self._height = PILImage.open(self.file).size
except Exception:
# probably the image is missing. nevermind.
pass
super(Image, self).save(*args, **kwargs)