How to upload a file via many2many relation?

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)

There are certain things that you do not required

here in license_0, max_length=255 is not required

another thing is that you are giving ForeignKey of user to License

also giving ManyToManyField of the License to User

Any one of them can work for the relation between these two models

Now to answer this first decide which relation in both of the above you need

I updated my question (added User_directory_path).

I have added m2m to bind users and their licenses and also ForeignKey for
creating a custom file upload path.