我想使用Django文件傳輸上傳文件,我想知道如何導入用戶名作爲上傳文件的名稱保存英寸現在我正在使用User.username,但這是無效的。我想最大的問題是如何從models.py訪問用戶的屬性。下面是我的models.py:Django文件傳輸保存到每個用戶的新文件
from django.db import models
from django.contrib.auth.models import User, UserManager
class UploadModel(User,models.Model):
title = models.CharField(max_length=64, blank=True)
user = models.ForeignKey(User)
file = models.FileField(upload_to='./uploads/'+str(User.username))
@property
def filename(self):
return self.file.name.rsplit('/', 1)[-1]
更新: 應用程序/上傳/ models.py
from django.db import models
from django.contrib.auth.models import User, UserManager
def uploadmodel_file_upload_to(instance, filename):
return 'uploads/%s/%s' % (instance.user.username, filename)
class UploadModel(models.Model):
user = models.ForeignKey('auth.user')
file = models.FileField(upload_to=uploadmodel_file_upload_to)
TemplateSyntaxError在/上傳 陷入DatabaseError同時呈現:列upload_uploadmodel.user_id不存在 LINE 1:選擇「upload_uploadmodel」。「id」,「upload_uploadmodel」。「用戶...
爲什麼UploadModel從用戶繼承? – jpic 2012-02-26 13:32:25