我試圖為我的django網(wǎng)絡(luò)應(yīng)用程序用戶的個(gè)人資料詳細(xì)信息構(gòu)建一個(gè)模型,例如:class UserDetails(models.Model): user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) profilePicture = models.ImageField(blank = True, upload_to='profile_pics/'+self.user.id+'/') country = models.CharField(max_length = 50, default='India') gender = models.CharField(max_length=10, default='NA') birthday = models.DateField(default=datetime.now()) phone = models.CharField(max_length=15)我在上面的模型中有一個(gè)圖像字段,我想將傳入的圖像上傳到我的媒體存儲(chǔ)路徑中的路徑。我試圖通過將圖像字段的屬性指定為 來做到這一點(diǎn)。我正在使用 AWS S3 進(jìn)行媒體存儲(chǔ),并且我已將設(shè)置中的必要設(shè)置設(shè)為:profile_pics/<id of the user whose profile is being set up>/upload_toupload_to = 'profile_pics/'+self.user.id+'/'AWS_ACCESS_KEY_ID = 'myaccesskeyid'AWS_SECRET_ACCESS_KEY = 'mysecretaccesskey'AWS_STORAGE_BUCKET_NAME = 'mybucketname'AWS_S3_FILE_OVERWRITE = FalseAWS_DEFAULT_ACL = NoneDEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'當(dāng)我嘗試進(jìn)行遷移時(shí),我收到以下錯(cuò)誤:Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv)請(qǐng)幫助我將圖像上傳到此模型的默認(rèn)路徑設(shè)置為 。profile_pics/<id of the user whose profile is being set up>/
1 回答

四季花海
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以將可調(diào)用傳遞給upload_to=...
參數(shù) [Django-doc]:
class UserDetails(models.Model):
def profile_picture_upload(self, filename):
return 'profile_pics/{}/{}'.format(self.user_id, filename)
# …
profilePicture = models.ImageField(blank=True, upload_to=profile_picture_upload)
注意:通常 Django 模型中字段的名稱是用snake_case編寫的,而不是 PerlCase,所以它應(yīng)該是:而不是配置文件圖片。profile_picture
添加回答
舉報(bào)
0/150
提交
取消