我有一個模型,其中我提出在tastypie的API。我有一個存儲路徑的字段,我手動維護的文件(由於用戶沒有上傳文件,我沒有使用FileField
)。這裏是一個模型的要點:的FileField在Tastypie
class FooModel(models.Model):
path = models.CharField(max_length=255, null=True)
...
def getAbsPath(self):
"""
returns the absolute path to a file stored at location self.path
"""
...
這裏是我的tastypie配置:
class FooModelResource(ModelResource):
file = fields.FileField()
class Meta:
queryset = FooModel.objects.all()
def dehydrate_file(self, bundle):
from django.core.files import File
path = bundle.obj.getAbsPath()
return File(open(path, 'rb'))
在文件領域的API這樣會返回一個文件的完整路徑。我希望tastypie能夠提供實際的文件或至少一個文件的URL。我怎麼做?任何代碼片斷,讚賞。
謝謝
哪裏'FileWrapper'從何而來? – 2012-11-26 03:00:02
'從django.core.servers.basehttp進口FileWrapper' – astevanovic 2012-11-26 12:50:56
這是否關閉文件句柄它被傳輸到客戶端後? – 2012-11-26 20:02:40