我有一个模型,我正在为其制作一个在 tastypie 中的 api。我有一个字段存储我手动维护的文件的路径(我没有使用 FileField
因为用户没有上传文件)。这是一个模型的要点:
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。我怎么做?感谢任何代码片段。
谢谢
最佳答案
首先确定一个 URL 方案,您的文件将如何通过 API 公开。您实际上并不需要 file 或 dehydrate_file(除非您想在 Tastypie 中更改模型本身的文件表示)。相反,只需在 ModelResource 上添加一个额外的操作。示例:
class FooModelResource(ModelResource):
file = fields.FileField()
class Meta:
queryset = FooModel.objects.all()
def override_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/download%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('download_detail'), name="api_download_detail"),
]
def download_detail(self, request, **kwargs):
"""
Send a file through TastyPie without loading the whole file into
memory at once. The FileWrapper will turn the file object into an
iterator for chunks of 8KB.
No need to build a bundle here only to return a file, lets look into the DB directly
"""
filename = self._meta.queryset.get(pk=kwargs[pk]).file
wrapper = FileWrapper(file(filename))
response = HttpResponse(wrapper, content_type='text/plain') #or whatever type you want there
response['Content-Length'] = os.path.getsize(filename)
return response
获取 .../api/foomodel/3/
返回: { ... '文件':'本地路径/文件名.ext', ...
获取 .../api/foomodel/3/下载/
返回: ...实际文件的内容...
或者,您可以在 FooModel 中创建一个非 ORM 子资源文件。您必须定义 resource_uri
(如何唯一标识资源的每个实例),并重写 dispatch_detail 以完全执行上述 download_detail 的操作。
关于python - Tastypie 中的文件字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449749/