5

Tastypie を使用した API の開発中に問題が発生しました。私が基本的に欲しいのは、json内でtastypieに画像を直接投稿する方法があるかどうかを知ることです.

私のモデルでは、現在 ImageField を使用しています:

  class MyClass(models.Model):
      description = models.TextField()
      user = models.ForeignKey(User)
      photo = models.ImageField(upload_to="image", null=True, blank=True)

次に、私のAPIファイルで:

    class InquiryResource(ModelResource):
        user = fields.ForeignKey(UserResource, 'user' ,full=True)
        photo = fields.FileField(attribute = 'photo', null=True, blank = True)

        class Meta :
            queryset = MyClass.objects.all()
            resource_name = "MyClass"
            authorization = Authorization()

これをユーザーと説明のみの基本的なjsonで送信すると、うまく機能します。次に、画像に関する情報を追加するとき:

    { ... ,
    photo : {
       Content-Type : "image/png",
       file : "base64string", <----- this one contains the file as a long string
       name : "test.png"
    } ...}

エラー メッセージが表示されます:「dict」オブジェクトには属性「_commited」がありません

Tastypie を使用してファイルをネイティブにアップロードする「クリーンな方法」はありますか、それとも Base64FileField を使用する必要がありますか?

ありがとうございました

4

1 に答える 1

0

はい、Base64FileField を使用できます: https://gist.github.com/jstarcher/ef8d91b8e8d058178f20

これについてもっと読む: https://github.com/toastdriven/django-tastypie/issues/42

于 2014-12-26T21:10:59.950 に答える