1

基本的に、を含むDjangoモデルを保存しようとしています。また、画像に含まれるEXIFデータから取得した値(存在する場合)でそのモデルとFloatFieldをImageField更新します。latitudelongitude

この問題を説明するサンプルモデルは次のとおりです。

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>

どんな助けでも大歓迎です。

注: save()でモデル全体を更新する方法だけで、EXIFデータの抽出や解析のサポートは必要ありません。

編集:さて、ファイルオブジェクトにアクセスして、レコードを保存する前にさらに処理できるようにします。

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 に答える