DRF: Upload many similar files

I have created a user API for uploading documents. The link to the file is stored in license_* type fields, the document status is stored in license_*_status.

models.py

@deconstructible
class User_directory_path(object):
    def __init__(self, prefix):
        self.prefix = prefix

    def __call__(self, instance, filename):
        return f'{instance.id}/{self.prefix}_{filename}'

class Profile(models.Model):
    class DocumentStatus(models.TextChoices):
        NOT_UPLOADED = 'NOT_UPLOADED'
        ON_REVIEW = 'ON_REVIEW'
        ACCEPTED = 'ACCEPTED'

    id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
    license_0 = models.FileField(verbose_name='License(1)',
                            storage=MinioBackend(bucket_name='django-backend-profiles-private', replace_existing=True),
                            upload_to=User_directory_path('license_0'), max_length=255, null=True, blank=True)
    license_0_status = models.CharField(verbose_name='License(1) Status',
                            max_length=15, choices=DocumentStatus.choices, default=DocumentStatus.NOT_UPLOADED)
    license_1 = models.FileField(verbose_name='License(2)',
                            storage=MinioBackend(bucket_name='django-backend-profiles-private', replace_existing=True),
                            upload_to=User_directory_path('license_1'), max_length=255, null=True, blank=True)
    license_1_status = models.CharField(verbose_name='License(2) Status',
                            max_length=15, choices=DocumentStatus.choices, default=DocumentStatus.NOT_UPLOADED)
    license_2 = models.FileField(verbose_name='License(3)',
                            storage=MinioBackend(bucket_name='django-backend-profiles-private', replace_existing=True),
                            upload_to=User_directory_path('license_2'), max_length=255, null=True, blank=True)
    license_2_status = models.CharField(verbose_name='License(3) Status',
                            max_length=15, choices=DocumentStatus.choices, default=DocumentStatus.NOT_UPLOADED)
    license_3 = models.FileField(verbose_name='License(4)',
                            storage=MinioBackend(bucket_name='django-backend-profiles-private', replace_existing=True),
                            upload_to=User_directory_path('license_3'), max_length=255, null=True, blank=True)
    license_3_status = models.CharField(verbose_name='License(4) Status',
                            max_length=15, choices=DocumentStatus.choices, default=DocumentStatus.NOT_UPLOADED)
    license_4 = models.FileField(verbose_name='License(5)',
                            storage=MinioBackend(bucket_name='django-backend-profiles-private', replace_existing=True),
                            upload_to=User_directory_path('license_4'), max_length=255, null=True, blank=True)
    license_4_status = models.CharField(verbose_name='License(5) Status',
                            max_length=15, choices=DocumentStatus.choices, default=DocumentStatus.NOT_UPLOADED)

In this example, the user can submit 5 licenses. The field names are similar, only the postfix changes. Should I use this approach when uploading a large number of similar files?

To be honest, you have used wrong relationship between licence and Profile. you should create a model called Licence and make a ManyToManyField on Licence Model.

Here is an example to clarify the subject.

Back to Top