From cec140e8683a602452b2ebda595924124b845bfc Mon Sep 17 00:00:00 2001 From: Pande Ketut Cahya Nugraha Date: Mon, 13 Aug 2018 11:03:03 +0700 Subject: [PATCH] use uuid as filename --- website/models.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/website/models.py b/website/models.py index 8f65a2cd..4130dd6c 100644 --- a/website/models.py +++ b/website/models.py @@ -1,10 +1,17 @@ from django.db import models from django.contrib.auth.models import User +import uuid +import os + +def get_file_path(instance, filename): + ext = filename.split('.')[-1] + new_filename = "%s.%s" % (uuid.uuid4(), ext) + return new_filename # Create your models here. class File(models.Model): - file = models.FileField(blank=False, null=False) + file = models.FileField(upload_to=get_file_path, blank=False, null=False) timestamp = models.DateTimeField(auto_now_add=True) class PostType(models.Model):