问题描述
我正在尝试在Django Python中实施帐户禁止。如果禁令是暂时的,则需要一个有效期。如果是永久性的,则到期日期应为null。我尝试使用CheckConstraint
来做到这一点,但是没有运气。当约束失败时,它将返回完整性错误。
def constraint__temporary_expires(ban_type,expires):
return (ban_type == "T" and expires is None) and (ban_type != "P")
class Ban(Moderation): # Moderation is a model
BAN_CHOICES = [
("P","Permanent"),("T","Temporary")
]
ban_type = models.CharField(choices=BAN_CHOICES,max_length=1,verbose_name="Type")
expires = models.DateTimeField(null=True,blank=True,help_text="This is ignored if ban is permanent")
class Meta:
constraints = [
models.CheckConstraint(
check=models.Q(
ban_type=constraint__temporary_expires(models.F("ban_type"),models.F("expires"))
),name="temporary_expires"
)
]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)