如何检查 Django (django-admin) 中的值转换? (重温)

发布于 2024-09-15 15:26:45 字数 1612 浏览 5 评论 0原文

我想知道如何控制模型数据的转换。我在 如何检查 Django 中的值转换 (django -admin)? 但是当我尝试在代码中实现它时,出现了问题(我能够不受惩罚地更改状态):

这是我的代码的相关部分:

# Blog Entry Draft Status Constants
ENTRY_STATUS_DRAFT = 0
ENTRY_STATUS_PUBLISHED = 1

# Create your models here.
class Blog(models.Model):
    title = models.CharField(max_length=200, unique=True)
    body_html = models.TextField(blank=True)
    pub_date = models.DateTimeField ('Date Published', blank=True, null=True, editable=False)
    PUB_STATUS = (
        (ENTRY_STATUS_DRAFT, 'Draft'),
        (ENTRY_STATUS_PUBLISHED, 'Published'),
    )
    status = models.IntegerField(choices=PUB_STATUS, default=0)

    def clean(self):
        # Don't allow draft entries to have a pub_date.
        if self.status == 'draft' and self.pub_date is not None:
            raise ValidationError('Draft entries may not have a publication date.')
        # Set the publication date for published items if it hasn't been set already
        if self.status == ENTRY_STATUS_PUBLISHED and not self.pub_date:
            self.pub_date = datetime.datetime.now()

    def clean_status(self):
        status = self.cleaned_data.get('status')
        if status == ENTRY_STATUS_DRAFT:
            if self.instance and self.instance.status == ENTRY_STATUS_PUBLISHED:
                raise ValidationError('You cannot change published to draft')
        return status

clean() 方法有效。我还尝试在 clean_status() 方法中使用“已发布”和“草稿”,但这不起作用。

我是否将 clean_status 放在正确的位置?我是否忽略了什么?

I was wondering about how to control transitions in model data. I found the solution at How to check value transition in Django (django-admin)? but when I tried to implement it within my code, something went wrong (I am able to change the status with impunity):

Here is the relevant portion of my code:

# Blog Entry Draft Status Constants
ENTRY_STATUS_DRAFT = 0
ENTRY_STATUS_PUBLISHED = 1

# Create your models here.
class Blog(models.Model):
    title = models.CharField(max_length=200, unique=True)
    body_html = models.TextField(blank=True)
    pub_date = models.DateTimeField ('Date Published', blank=True, null=True, editable=False)
    PUB_STATUS = (
        (ENTRY_STATUS_DRAFT, 'Draft'),
        (ENTRY_STATUS_PUBLISHED, 'Published'),
    )
    status = models.IntegerField(choices=PUB_STATUS, default=0)

    def clean(self):
        # Don't allow draft entries to have a pub_date.
        if self.status == 'draft' and self.pub_date is not None:
            raise ValidationError('Draft entries may not have a publication date.')
        # Set the publication date for published items if it hasn't been set already
        if self.status == ENTRY_STATUS_PUBLISHED and not self.pub_date:
            self.pub_date = datetime.datetime.now()

    def clean_status(self):
        status = self.cleaned_data.get('status')
        if status == ENTRY_STATUS_DRAFT:
            if self.instance and self.instance.status == ENTRY_STATUS_PUBLISHED:
                raise ValidationError('You cannot change published to draft')
        return status

The clean() method works. I also tried using 'Published' and 'Draft' in the clean_status() method but that didn't work.

Am I putting clean_status in the right place? Am I overlooking something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-09-22 15:26:45

您的 clean 方法应返回 self.cleaned_data,就像 clean_status 返回 self.cleaned_data['status'] 一样。 :)

Your clean method should return the self.cleaned_data just like the clean_status returns the self.cleaned_data['status']. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文