how to change the filename stored on disk - django, s3

77
November 30, 2019, at 9:10 PM

I am trying to change the name of a file that a user uploads. I can get it to change the filename, but it is still stored in the S3 server as the original filename.

Django says that -- "The file is saved as part of saving the model in the database, so the actual file name used on disk cannot be relied on until after the model has been saved."

You guys know of any tips on where do put a function in either my view, form or model to allow for me to change the filename on the disk?

from forms.py:

class AddAttachmentForm(forms.ModelForm):
    class Meta:
        model = Attachment
        fields = ('attachment',)
        labels = {
            'attachment': ''
        }
        widgets = {
            'attachment': forms.FileInput(attrs={'style':'display:none;'})
        }
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            field.error_messages = {
                'required': '{fieldname} is required'.format(fieldname=field.label),
            }

from models.py

class Attachment(models.Model):
    def rand_str(n): return ''.join([random.choice(string.ascii_letters + string.digits) for i in range(n)])
    random_folder = rand_str(75)
    user = models.ForeignKey('User', on_delete=models.CASCADE, null=True)
    competency = models.ForeignKey('Competency', related_name="attachments", on_delete=models.CASCADE)
    attachment = models.FileField(upload_to=random_folder, blank=True)
    attacher = models.ForeignKey('User', related_name='attacher', on_delete=models.SET_NULL, null=True)
    filename = models.CharField(max_length=255)
    file_type = models.CharField(max_length=255, null=True, blank=True)
    date_attached = models.DateTimeField(null=True, blank=True)

from views.py

        form = AddAttachmentForm(request.POST, request.FILES)
        if form.is_valid():
            attachment = form.save(commit=False)
            attachment.user = student
            attachment.attacher = self.request.user
            attachment.date_attached = timezone.now()
            attachment.competency = competency
            attachment.filename = request.FILES['attachment'].name
            attachment.save()
READ ALSO
How to configure ttk progressbar in GUI for downloading from web

How to configure ttk progressbar in GUI for downloading from web

How to connect progress bar with loading bytes (video) in GUI mode? in console mode, the scripts that I found work perfectly (tqdm, shutil, click)Everything is displayed correctly

116
mismatch in sizes with test and train data during function print

mismatch in sizes with test and train data during function print

Data file I would like to process has 71 records build with two columns: one for x value and second one for y valueMain task is to select training part and testing part, print chosen functions (in my example I've taken linear and exponential(^4) one

95
3D plotting in Python - Adding a Legend to Scatterplot

3D plotting in Python - Adding a Legend to Scatterplot

Essentially I'd like to add a legend for the scatterplot that shows the unique values in y_train and what color point it corresponds to on the plot

79
Get combobox value to feed into button command in python tkinter

Get combobox value to feed into button command in python tkinter

so I'm learning tkinter by building a "weightlifting GUI" for funAs it stands, the application mostly runs

77