如何检查 Django (django-admin) 中的值转换? (重温)
我想知道如何控制模型数据的转换。我在 如何检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 clean 方法应返回
self.cleaned_data
,就像clean_status
返回self.cleaned_data['status']
一样。 :)Your clean method should return the
self.cleaned_data
just like theclean_status
returns theself.cleaned_data['status']
. :)