简体   繁体   English

Django unique_together自定义错误消息

[英]Django unique_together custom error message

I'm trying to create a custom error message for unique_together: 我正在尝试为unique_together创建自定义错误消息:

class Recipient(models.Model):
  mobile = PhoneNumberField()
  mobile2 = PhoneNumberField()

  class Meta:
     unique_together = (("mobile", "mobile2"),)


  def unique_error_message(self, model_class, unique_check):
    print("I don't seem to run")
    if model_class == type(self) and unique_check == ('mobile', 'mobile2'):
        return 'My custom error message'
    else:
        return super(Recipient, self).unique_error_message(model_class, unique_check)

However my error message is not running instead I get: 但是我的错误消息没有运行而是得到:

Duplicate entry '+4473192817212-+4478192817210' for key 'mobile'\\" 密钥“ mobile”的条目“ + 4473192817212- + 4478192817210”重复

why? 为什么?

The error message you pasted seems more like an error caused by a unique constraint on the mobile field of the recipient table, indicating that at one point you had unique=True at one point. 您粘贴的错误消息似乎更像是由recipient表的mobile字段上的唯一约束引起的错误,表明在某一点上您具有unique=True Check for this constraint in the database or if this is just in development, delete the database and syncdb again. 检查数据库中是否存在此约束,或者是否正在开发中,请删除数据库并再次syncdb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM