python - using a if statement inside django model that is updating the model when threshold is reached
问题描述
Please bear with me as I'm new with Django
I have in the model a table where I need to put a timer. If the timer limit is reached, we update the row of the column accordingly.
This is my model
class Question(models.Model):
date = models.DateTimeField(auto_now_add=True, verbose_name=_('date'
))
first_name = models.CharField(max_length=255,
verbose_name=_('first name'),
blank=True)
last_name = models.CharField(max_length=255,
verbose_name=_('last name'),
blank=True)
email = models.EmailField(max_length=255,
verbose_name=_('email address'),
blank=True)
phone = models.CharField(max_length=255, verbose_name=_('phone'),
blank=True)
extra = models.CharField(max_length=255, verbose_name=_('extra'),
blank=True)
subject = models.CharField(max_length=255, verbose_name=_('subject'
), blank=True)
message = models.TextField(max_length=1000,
verbose_name=_('question'))
relevance = models.BooleanField(default=True,
verbose_name=_('relevance'))
marketing_consent = models.BooleanField(default=False,
verbose_name=_('marketing consent'))
expert = models.ForeignKey(Expert, on_delete=models.SET_NULL,
null=True, blank=True,
verbose_name=_('expert (target)'))
analysis_tags = models.ManyToManyField(AnalysisTag, blank=True,
verbose_name=_('analysis tags'))
predicted_experts = models.ManyToManyField(Expert,
through='AssociatedExpert', related_name='predicted_for',
blank=True)
public_token = models.CharField(max_length=50,
default=generate_token,
db_index=True, unique=True,
verbose_name=_('public token'))
STATE_CHOICES = (
('W', 'Waiting'),
('D', 'Declined'),
('T', 'Taken'),
('A', 'Answered'),
('F', 'Feedback'),
('P', 'Pending'),
('C', 'Cancelled'),
)
state = models.CharField(max_length=10, choices=STATE_CHOICES,
default='P', verbose_name=_('state'))
What I want to do is to add the following but I'm unsure if the model is the right place to add the method below and also if I'm writing correctly that.
Update : I want a check in the background and mark the questions as Cancelled
if the questions reach the time limit of 48 minutes
def value(self):
48_mn_ago = now() - timedelta(minutes=48)
if self.date > 48_mn_ago and self.state == 'P':
return self.state = 'C', self.first_name = '***', self.last_name = '***'
Many thanks for your help.
解决方案
让自定义方法或属性与模型中的实例一起操作是非常好的,它们可以用作常规的 Python 类。但我有点不确定你想做什么作为回报。如果需要更改对象(行),则应更改其字段并调用 self.save()
def update_if_rotten(self):
48_mn_ago = now() - timedelta(minutes=48)
if self.date > 48_mn_ago and self.state == 'P':
self.state = 'C'
self.first_name = '***'
self.last_name = '***'
self.save()
用法:
question = Question.objects.create(...)
question.update_if_rotten()
推荐阅读
- django - django-filter 上的 ModelChoiceFilter
- matlab - 从图像中提取 CMY 颜色分量
- javascript - 我想显示一个页面的数据并使用 chrome 扩展显示到网站的另一个页面中?
- powerbi - 显示正确的目标线
- wget - wget 递归下载所有子目录中的特定文件类型
- scala - Spark:以指定的日期格式读取 csv
- flutter - Flutter 应用程序上的 flutter_dotenv 出现空安全错误
- php - 购买多件商品,但只扣入钱包一次
- python - 使用引用自身的类型进行键入
- python - 如何在 WD mycloud 中运行 python 代码?