1

基本上,我正在尝试保存一个包含 的 Django 模型ImageField,并使用从图像中包含的 EXIF 数据中获取的值(如果有)更新其latitudelongitudeFloatFields。

这是说明问题的示例模型:

class GeoImage(models.Model):
    image = models.ImageField(upload_to='/path/to/uploads')
    latitude = models.FloatField(null=True, blank=True)
    longitude = models.FloatField(null=True, blank=True)

    def save(self):
        # grab the path of the image of the ImageField
        # parse the EXIF data, fetch latitude and longitude
        self.latitude = fetched_latitude
        self.longitude = fetched_longitude
        return super(GeoImage, self).save()

你能发现问题吗?我不知道如何在模型实例实际保存之前访问图像文件路径,我无法保存记录,更新一些属性然后再次保存它,因为它会创建一个 post_save 循环(理论上也是如此一个post_save信号……</p>

非常感谢任何帮助。

注意:我不需要 EXIF 数据提取或解析方面的帮助,只需在 save() 上更新整个模型即可。

编辑:好的,因此您可以在保存记录之前访问文件对象并进一步处理它:

class GeoImage(models.Model):
    image = models.ImageField(upload_to='/path/to/uploads')
    latitude = models.FloatField(null=True, blank=True)
    longitude = models.FloatField(null=True, blank=True)

    def save(self):
        latitude, longitude = gps_utils.process_exif(self.image.file)
        if latitude: self.latitude = latitude
        if longitude: self.longitude = longitude
        return super(GeoImage, self).save(*args, **kwarg)
4

1 回答 1

1

FileField 应该返回一个类似文件的对象,您可以读取该对象以提取 exif 信息。

于 2010-12-19T05:19:56.590 回答