英文:
How to get object ID for custom file upload path?
问题
Sure, here is the translated code snippet:
我正在尝试获取一个保存文件路径的方法,类似于`<employee_id>/<prefix>_<filename>`。我如何在`User_directory_path`类中获取`<employee_id>`?
在这段代码中,我只能获取到`<license_id>`:
```python
import uuid
from django.db import models
from django.utils.deconstruct import deconstructible
from django_minio_backend import MinioBackend
@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 DocumentStatus(models.TextChoices):
NOT_UPLOADED = 'NOT_UPLOADED'
ON_REVIEW = 'ON_REVIEW'
class License(models.Model):
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-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-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)
class Employee(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
licenses = models.ManyToManyField(License, blank=True)
Is there anything else you would like to know or clarify?
英文:
I'm trying to get a path to save a file like <employee_id>/<prefix>_<filename>. How do I get the <employee_id> in the User_directory_path class?
In this code I can only get <license_id>:
import uuid
from django.db import models
from django.utils.deconstruct import deconstructible
from django_minio_backend import MinioBackend
@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 DocumentStatus(models.TextChoices):
NOT_UPLOADED = 'NOT_UPLOADED'
ON_REVIEW = 'ON_REVIEW'
class License(models.Model):
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-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-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)
class Employee(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
licenses = models.ManyToManyField(License, blank=True)
答案1
得分: 1
以下是翻译好的部分:
"employee_id" 没有意义:您使用了一个 "ManyToManyField",因此可以有零个、一个或多个 "Employee",即使确实有一个,这两者也仅在 "License" 和 "Employee" 构建之后才会链接。
因此,建模似乎不正确,您可能打算使用以下方式:
class License(models.Model):
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-private', replace_existing=True
),
upload_to=UserDirectoryPath('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-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,
)
employee = models.ForeignKey('Employee', on_delete=models.CASCADE)
class Employee(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
在这种情况下,您使用:
@deconstructible
class UserDirectoryPath(object):
def __init__(self, prefix):
self.prefix = prefix
def __call__(self, instance, filename):
return f'{instance.employee_id}/{self.prefix}_{filename}'
注意:请使用
UUIDField来存储 UUID,而不是CharField,这可以进行验证,以检查输入的数据是否确实是 UUID。
英文:
It makes no sense to have an employee_id: you use a ManyToManyField, so there can be zero, one or more Employees, and even if there is exactly 9one, the two are linked only after construction of the License and Employee.
The modeling thus looks wrong, likely you intended to work with:
<pre><code>class License(models.Model):
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-private', replace_existing=True
),
upload_to=UserDirectoryPath('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-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,
)
<b>employee</b> = models.ForeignKey(<b>'Employee'</b>, on_delete=models.CASCADE)
class Employee(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)</code></pre>
in which case, you use:
<pre><code>@deconstructible
class UserDirectoryPath(object):
def init(self, prefix):
self.prefix = prefix
def __call__(self, instance, filename):
return f'{instance<b>.employee_id</b>}/{self.prefix}_{filename}'</code></pre>
> Note: Please use an UUIDField <sup>[Django-doc]</sup> instead of a CharField <sup>[Django-doc]</sup> to store a UUID, this can do validation to check if the entered data is indeed a UUID.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论