我有一个带有ImageField的模型和一个更改上传路径的函数
class Images(models.Model):
def upload_path(self, filename):
return os.path.join(settings.TOOLS_IMAGE_UPLOADS, self.get_artworkID(),
filename)
image = models.ImageField(upload_to=upload_path)
artworkID = models.IntegerField(null=True, blank=True)
def __str__(self):
if self.title == "" or not self.title:
return str(self.image)
return self.title
def get_artworkID(self):
if self.artworkID:
return str(self.artworkID)
return 'temp'
TOOLS_IMAGE_UPLOADS
的值是
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.dirname(PROJECT_DIR)
MEDIA_NAME_FOLDER = 'media'
MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_NAME_FOLDER)
TOOLS_IMAGE_UPLOADS = os.path.join(MEDIA_ROOT, 'uploads', 'images')
问题是通过Django admin或rest api可视化Image对象时返回的URL是错误的
路径返回:
http://localhost:8000/media/webapp/artrights/media/uploads/images/temp/file.png
文件的真实路径:
http://localhost:8000/media/uploads/images/temp/file.png
我做错了什么或者我只需要制作一个自定义函数来返回正确的路径?
答案 0 :(得分:1)
upload_to
可调用{/ 3}}应该return a path相对于MEDIA_ROOT
,因为它会附加到TOOLS_IMAGE_UPLOADS
。
我认为问题是MEDIA_ROOT
已包含MEDIA_ROOT
。
所以函数应该在def upload_path(self, filename):
return os.path.join('uploads', 'images', self.get_artworkID(), filename)
:
df = read.table(text = "
x y z
1 2 3
1 2 3
1 2 3
2 3 NA
1 2 3
1 3 NA
", header= T)
library(tidyverse)
# get the column names of your dataset
names = names(df)
# get unique values after omitting rows with NAs
value = unlist(unique(na.omit(df)))
# create a dataset with names and values
# (this is the pattern you want to follow)
df3 = data.frame(names, value)
df %>%
mutate(id = row_number()) %>% # flag the row number
gather(v,value,-id) %>% # reshape
na.omit() %>% # remove rows with NAs
left_join(df3, by="value") %>% # join info about your pattern
select(-v) %>% # remove that column
spread(names, value) %>% # reshape
select(-id) # remove row number
# x y z
# 1 1 2 3
# 2 1 2 3
# 3 1 2 3
# 4 NA 2 3
# 5 1 2 3
# 6 1 NA 3