[Django]-Unique filename of uploaded file using the django FORM

2👍

In your Model you can set the upload_to of the imagefield to a function and then generate the uuid.

A very simple (untested) example:

import uuid

Class MyModel(models.Model):
    def get_path(instance, filename):
        extension = filename.split('.')[-1]
        uuid = uuid.uuid1().hex
        return f'path/to/file/{uuid}.{extension}'

    image = ImageField(upload_to=get_path)

0👍

What I understand of your problem, you can set initial for FORM class when initialising it. like:

help_guide_form = HelpGuideForm(initial={'headline': uuid.uuid4().hex}, instance= Helpguide)

from django docs. Also see the initial reference.

👤Azeem

Leave a comment