如何获取自定义文件上传路径的对象ID?

huangapple go评论84阅读模式
英文:

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 &lt;employee_id&gt;/&lt;prefix&gt;_&lt;filename&gt;. How do I get the &lt;employee_id&gt; in the User_directory_path class?

In this code I can only get &lt;license_id&gt;:

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&#39;{instance.id}/{self.prefix}_{filename}&#39;
class DocumentStatus(models.TextChoices):
NOT_UPLOADED = &#39;NOT_UPLOADED&#39;
ON_REVIEW = &#39;ON_REVIEW&#39;
class License(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
license_0 = models.FileField(
verbose_name=&#39;License(1)&#39;,
storage=MinioBackend(
bucket_name=&#39;django-backend-private&#39;,
replace_existing=True),
upload_to=User_directory_path(&#39;license_0&#39;),
max_length=255,
null=True,
blank=True,
)
license_0_status = models.CharField(
verbose_name=&#39;License(1) Status&#39;,
max_length=15,
choices=DocumentStatus.choices,
default=DocumentStatus.NOT_UPLOADED)
license_1 = models.FileField(
verbose_name=&#39;License(2)&#39;,
storage=MinioBackend(
bucket_name=&#39;django-backend-private&#39;,
replace_existing=True),
upload_to=User_directory_path(&#39;license_1&#39;),
max_length=255,
null=True,
blank=True,
)
license_1_status = models.CharField(
verbose_name=&#39;License(2) Status&#39;,
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&#39;{instance&lt;b&gt;.employee_id&lt;/b&gt;}/{self.prefix}_{filename}&#39;&lt;/code&gt;&lt;/pre&gt;

> Note: Please use an UUIDField&nbsp;<sup>[Django-doc]</sup> instead of a CharField&nbsp;<sup>[Django-doc]</sup> to store a UUID, this can do validation to check if the entered data is indeed a UUID.

huangapple
  • 本文由 发表于 2023年7月6日 19:54:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定