[英]How to identify if a field's choices have changed in django model?
I have a model like this:我有一个像这样的 model:
class MyModel(models.Model):
status_choices = (
('in_progress', 'In Progress'),
('in_review', 'In Review'),
('active', 'Active'),
)
# I want to detect when these choices are changed and trigger a function
status = models.CharField(choices=status_choices, max_length=20, blank=True)
# other model fields
Here the status choices can be changed, and I want to detect if the choices have changed and then trigger a function call.这里可以更改状态选项,我想检测选项是否已更改,然后触发 function 调用。
I know there is post_init
listener, but how can I compare my cuurent choices with the previous choices when initializing the model?我知道有post_init
侦听器,但是在初始化 model 时,如何将我的当前选择与以前的选择进行比较?
In that case, one typically writes a model, like MyModelStatus
, and work with a ModelChoiceField
instead.在这种情况下,通常会编写一个 model ,例如MyModelStatus
,并使用ModelChoiceField
代替。
so something like:所以像:
class MyModelStatus(models.Model):
name = models.CharField(max_length=20, unique=True)
def __str__(self):
return self.name
class MyModel(models.Model):
status = models.ForeignKey(MyModelStatus, on_delete=models.PROTECT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.