我有一个模型,我正在为此制作一个美味的 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
"""
...
这是我的美味派配置:
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 中,这将返回文件的完整路径。我希望tastepie 能够提供实际文件或至少一个文件的url。我怎么做?任何代码片段表示赞赏。
谢谢