4

i am trying to makemigrations to uploadExcel but an error shows up that says

ValueError: Could not find function wrapper in uploadExcel.models.

this is the code I am using in my uploadExcel.models.

from django.db import models
import os

def path_and_rename(path):
    def wrapper(instance, filename):
        ext = filename.split('.')[-1]
        if instance.pk:
            filename = '{}.{}'.format(instance.pk, ext)
        else:
            filename = '{}.{}'.format('data', ext)
        return os.path.join(path, filename)
    return wrapper



class ExcelUploadModel(models.Model):

    file_name = models.CharField(max_length=255, blank=True)
    document = models.FileField(upload_to=path_and_rename('test/'))
    uploaded_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.file_name

    def delete(self,*args, **kwargs):
        self.document.delete()
        super().delete(*args, **kwargs)

any help will be very appreciated thank you best,

Cesar Rodriguez
  • 192
  • 1
  • 15

1 Answers1

3

Try this

from django.utils.deconstruct import deconstructible

@deconstructible
class PathRename(object):

    def __init__(self, sub_path):
        self.path = sub_path

    def __call__(self, instance, filename):
        ext = filename.split('.')[-1]
        # set filename as random string
        filename = '{}.{}'.format(uuid4().hex, ext)
        # return the whole path to the file
        return os.path.join(self.path, filename)

path_and_rename = PathRename("/test")

Then

class ExcelUploadModel(models.Model):
    document = models.FileField(upload_to=path_and_rename)
shafik
  • 6,098
  • 5
  • 32
  • 50