Skip to content
Snippets Groups Projects
Commit 93cb054e authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Rename files on upload

parent fb505b75
No related branches found
No related tags found
1 merge request!86Merge school-apps
import os
from uuid import uuid4
def path_and_rename(instance, filename):
upload_to = 'menus'
ext = filename.split('.')[-1]
# get filename
if instance.pk:
filename = '{}.{}'.format(instance.pk, ext)
else:
# set filename as random string
filename = '{}.{}'.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(upload_to, filename)
# Generated by Django 2.0.7 on 2018-11-29 16:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mealplan', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='mealplan',
name='pdf',
field=models.FileField(upload_to='menus/'),
),
]
# Generated by Django 2.0.7 on 2018-11-29 16:47
from django.db import migrations, models
import helper
class Migration(migrations.Migration):
dependencies = [
('mealplan', '0002_auto_20181129_1743'),
]
operations = [
migrations.AlterField(
model_name='mealplan',
name='pdf',
field=models.FileField(upload_to=helper.path_and_rename),
),
]
from django.db import models
# Create your models here.
from helper import path_and_rename
class MealPlan(models.Model):
calendar_week = models.IntegerField()
year = models.IntegerField()
pdf = models.FileField(upload_to="menus/")
pdf = models.FileField(upload_to=path_and_rename)
def __str__(self):
return "KW {}/{}".format(self.calendar_week, self.year)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment