DRF: Загружать много похожих файлов
Я создал пользовательский API для загрузки документов. Ссылка на файл хранится в полях типа license_*, статус документа хранится в 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)
В этом примере пользователь может представить 5 лицензий. Имена полей похожи, меняется только постфикс. Должен ли я использовать этот подход при загрузке большого количества одинаковых файлов?
Честно говоря, вы использовали неправильные отношения между licence
и Profile
.
Вы должны создать модель с именем Licence
и сделать поле ManyToManyField.
на Licence
Model.
Вот пример для разъяснения темы.