我喜欢澄清我是Django的新手。
Versions:
Django 1.5
Python 2.7
PostgreSQL 9.3
我有一个将文件上传到AWS S3的webapp,目前正在运行。我想将文件命名为contentid
字段的名称,该字段在上传时自动分配V4 UUID。我会尝试挑选相关信息并在此处发布。我不确定在哪里收集这些信息并将其声明为名称。
查看
def upload_content(request):
if request.method == 'POST':
form = ContentForm(request.POST, request.FILES)
if form.is_valid():
new_content = Content(name=request.POST['name'],accountid=request.user.id,public=False,url=request.POST['name'],uploaddate=datetime.now(),viewcount='0',file = request.FILES['file'])
new_content.save()
return HttpResponseRedirect('/Console/Content/')
模型
class Content(models.Model):
name = models.CharField(max_length=128)
accountid = models.IntegerField(max_length=34)
url = models.CharField(max_length=200)
uploaddate = models.DateTimeField('date published')
viewcount = models.IntegerField(max_length=34)
public = models.BooleanField(max_length=1)
contentid = UUIDField(unique=True,editable=False)
file = models.FileField(upload_to='content')
@classmethod
def get_content_list(cls, account):
cursor = connection.cursor()
cursor.execute('SELECT name, contentid, public, uploaddate, id FROM webapp_content WHERE accountid=%s ORDER BY uploaddate', [account])
ret = cursor.fetchall()
return ret
答案 0 :(得分:0)
所以我有一些答案,但我仍然有问题
模型:
def generate_new_filename(instance, filename):
ext = filename.split('.')[-1]
filename = '{}.{}'.format(uuid.uuid4().hex, ext)
return (filename)
class Content(models.Model):
file = models.FileField(upload_to=generate_new_filename)
视图:
仍然和上面一样,我怎么写我的sql新保存的文件名代替request.FILES['file']
结果:
文件get写的是正确的变量,url不是