Функция:
def custom_path(path):
new_path = os.path.abspath(path)
def get_path():
return new_path
return get_path
Проблема в custom_path
в том, что при вызове внутри этой функции get_image_path
создается каталог с абсолютным путем, например <function custom_path at 0x7f432d99e158>/somedir/finalName.*ext
, но я хочу, чтобы он возвращалсякак строковое представление.Мой код:
def get_image_path(instance, file):
'''
saves file to some location
'''
file_path = custom_path*
new_file = random.randint(1, 45360789120)
name, ext = get_file_ext(file)
ranstr = random_string_generator(size=4)
final_ = f'{ranstr}_{new_file}{ext}'
return f'{file_path}/{new_file}/{final_}'
Отправитель:
from ... import custom_path, get_image_path
custom_path('posts')
class Post(models.Model):
...
...
image = models.FileField(upload_to=get_image_path, null=True,
blank=True, verbose_name='article image (optional)')
...
...
В противном случае я должен изменить эту функцию как ...
def get_image_path(instance, file):
...
...
return f'posts*/{new_file}/{final_}' # and so on for *different models..
Я также пыталсякак то так ...
def get_image_path(instance, file):
...
...
return f'{new_file}/{final_}'
base = os.path.dirname('posts/')
upload_path = base + os.path.join(str(get_image_path))
class Post(models.Model):
...
...
image = models.FileField(upload_to=upload_path, null=True,
blank=True, verbose_name='article image (optional)')
...
...
выводит: ''/newDir/newFile.*ext
, как от этого избавиться??️?️