上传图像时基于id/用户名的Django动态文件夹名称
你好,再见 我想根据上传者的ID/用户名重命名我的文件夹名称 我的设置如下:上传图像时基于id/用户名的Django动态文件夹名称,django,Django,你好,再见 我想根据上传者的ID/用户名重命名我的文件夹名称 我的设置如下: class Account(AbstractBaseUser): username= models.CharField(...) # As per our setup, user cant enter their own username but will be auto created based on a separate py file on my view during registration
class Account(AbstractBaseUser):
username= models.CharField(...) # As per our setup, user cant enter their own username but will be auto created based on a separate py file on my view during registration
...
class Type01Account(models.Model):
account = models.OneToOneField(Account, on_delete=models.CASCADE)
...
imgPath = ""
images= models.ImageField(upload_to=imgPath, null=False, blank=False)
我应该如何分配imgPath?我应该在模型上还是在视图上指定它?如果是,我应该如何保存它?upload\u to参数可以是一个函数,它接受模型实例和文件名并返回上传路径:
def img_path(instance, filename):
return f"{instance.account.username}/{filename}"
class Type01Account(models.Model):
account = models.OneToOneField(Account, on_delete=models.CASCADE)
...
imgPath = ""
images= models.ImageField(upload_to=img_path, null=False, blank=False)
参考:upload\u to参数可以是一个函数,它接受模型实例和文件名并返回上传路径:
def img_path(instance, filename):
return f"{instance.account.username}/{filename}"
class Type01Account(models.Model):
account = models.OneToOneField(Account, on_delete=models.CASCADE)
...
imgPath = ""
images= models.ImageField(upload_to=img_path, null=False, blank=False)
参考:处理包含文件的任何字段的所有上载到的类
import os
from django.utils import timezone
class UploadTo:
def __init__(self, name):
self.name = name
def __call__(self, instance, filename):
base_filename, file_extension = self.generate_name(filename)
path = f'files/{instance.__class__.__name__}/{self.name}/{timezone.now().strftime("%y-%m-%d")}/{base_filename}{file_extension}'
# any custom action on path
return path
def generate_name(self, filename):
base_filename, file_extension = os.path.splitext(filename)
# any custom action on file name
return base_filename, file_extension
def deconstruct(self):
# you can add some static value from model like `self.name`
return 'path.to.this.UploadTo', [self.name], {}
# Usage example
class ExampleModel(models.Model):
# add here some static value to class `news_image`
news_image = models.ImageField(_("news image"), upload_to=UploadTo('news_image'), blank=True)
用于处理包含文件的任何字段的所有上载到的类
import os
from django.utils import timezone
class UploadTo:
def __init__(self, name):
self.name = name
def __call__(self, instance, filename):
base_filename, file_extension = self.generate_name(filename)
path = f'files/{instance.__class__.__name__}/{self.name}/{timezone.now().strftime("%y-%m-%d")}/{base_filename}{file_extension}'
# any custom action on path
return path
def generate_name(self, filename):
base_filename, file_extension = os.path.splitext(filename)
# any custom action on file name
return base_filename, file_extension
def deconstruct(self):
# you can add some static value from model like `self.name`
return 'path.to.this.UploadTo', [self.name], {}
# Usage example
class ExampleModel(models.Model):
# add here some static value to class `news_image`
news_image = models.ImageField(_("news image"), upload_to=UploadTo('news_image'), blank=True)
嘿对不起,我花了这么长时间才回复。img_path功能是否在考虑范围内?如果是,在Type01Account上,img_路径未定义。保存py文件时返回错误消息。如果在Type01Account下,则当我上载图像时,非类型对象没有属性“username”。@rodjames img_路径将与Type01Account位于同一个文件中,就在它上面。此外,非类型对象没有属性“username”,这意味着在保存Type01Account实例时,您没有将Type01Account的帐户属性设置为account实例。在我添加此def img_路径之前,保存时没有非类型错误消息。在我添加img_path之后,它现在正在呈现错误消息。也许我的看法有问题。让我查一下,我知道了。而不是instance.account。你好,又是我。返回f{instance.account.username}/{filename}正在进行注册,但在我的更新中,相同的表单不同的视图。嘿。对不起,我花了这么长时间才回复。img_path功能是否在考虑范围内?如果是,在Type01Account上,img_路径未定义。保存py文件时返回错误消息。如果在Type01Account下,则当我上载图像时,非类型对象没有属性“username”。@rodjames img_路径将与Type01Account位于同一个文件中,就在它上面。此外,非类型对象没有属性“username”,这意味着在保存Type01Account实例时,您没有将Type01Account的帐户属性设置为account实例。在我添加此def img_路径之前,保存时没有非类型错误消息。在我添加img_path之后,它现在正在呈现错误消息。也许我的看法有问题。让我查一下,我知道了。而不是instance.account。你好,又是我。返回的f{instance.account.username}/{filename}正在进行注册,但是在我的更新中,相同的表单不同的视图。