I have two models that are connected via m2m:
@deconstructible
class User_directory_path(object):
def __init__(self, prefix):
self.prefix = prefix
def __call__(self, instance, filename):
return f'{instance.user_id}/{self.prefix}_{filename}'
class License(models.Model):
license_0 = models.FileField(
storage=MinioBackend(
bucket_name='django-backend-private',
replace_existing=True),
upload_to=User_directory_path('license_0'),
max_length=255,
null=True,
blank=True,
)
user = models.ForeignKey('User', on_delete=models.CASCADE)
class User(models.Model):
licenses = models.ManyToManyField(License, blank=True, related_name='user_licenses',)
Is it possible to implement file uploads only through the api/users/
endpoint?
Test example:
class CreateUserTest(APITestCase):
def setUp(self):
self.url = f'{host}/api/users/'
self.data = {
'id': user_id,
'licenses': {
'license_0': open(f'{test_data}/important_document.pdf', 'rb'),
}
}
def test_create_user(self):
response = self.client.post(self.url, self.data, format='multipart', HTTP_AUTHORIZATION=token)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)