【发布时间】:2020-12-04 07:05:08
【问题描述】:
我用 DRF 实现了一个文件上传端点,问题是文件没有显示文件的绝对 url,如下图所示。我希望绝对网址以http://localhost ....
这是我的 django 设置
STATIC_URL = '/static/'
# The folder hosting the files
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
## Serving the STATIC FILES
# As declared in NginX conf, it must match /src/static/
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'static')
MEDIA_URL = '/media/'
# do the same for media files, it must match /opt/services/djangoapp/media/
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
models.py
class Document(models.Model):
"""This represents document class model."""
file = models.FileField(upload_to='documents/inspections/%Y/%m/%d')
timestamp = models.DateTimeField(auto_now_add=True)
@property
def name(self):
name = self.file.name[33:]
return name
【问题讨论】:
-
除非您有权访问
request对象,否则您无法在模型级别执行此操作。最好在序列化器上这样做 -
请添加你的序列化器,也许我可以试试
标签: django django-rest-framework