Django模型:Email字段是唯一的,如果不是null/blank



假设您有一个简单的模型:

Class Contact(models.Model):
    email = models.EmailField(max_length=70,blank=True)
    first = models.CharField(max_length=25,blank=True)
    last = models.CharField(max_length=25,blank=True)

我想做的是将电子邮件设置为唯一的,然而,这样做我必须使它排除空白的电子邮件地址——我不希望这样。

我一直在想这样的事情,但我想知道是否有更好的方法来处理它。

from django.core.validators import email_re
from django.core.exceptions import ValidationError
def save(self, *args, **kwargs):
    # ... other things not important here
    self.email = self.email.lower().strip() # Hopefully reduces junk to ""
    if self.email != "": # If it's not blank
        if not email_re.match(self.email) # If it's not an email address
            raise ValidationError(u'%s is not an email address, dummy!' % self.email)
        if Contact.objects.filter(email = self.email) # If it already exists
            raise ValidationError(u'%s already exists in database, jerk' % self.email) 
    super(Contact, self).save(*args, **kwargs)

有更好的方法吗?

不幸的是,这并不像设置null=True, unique=True, blank=True那么简单。每当你尝试使用csv或其他基于文本的源文件导入时,Django的某些部分,为了唯一性,会将"视为不应该被复制的东西。

解决方法是覆盖保存方法,如下所示:
def save(self, *args, **kwargs):
    # ... other things not important here
    self.email = self.email.lower().strip() # Hopefully reduces junk to ""
    if self.email != "": # If it's not blank
        if not email_re.match(self.email) # If it's not an email address
            raise ValidationError(u'%s is not an email address, dummy!' % self.email)
    if self.email == "":
        self.email = None
    super(Contact, self).save(*args, **kwargs)

然后,使用unique, null和blank将按预期工作。

Class Contact(models.Model):
    email = models.EmailField(max_length=70,blank=True, null= True, unique= True)

就这样做:

class Contact(models.Model):
    email = models.EmailField(max_length=70, null=True, blank=True, unique=True)

我尝试使用保存,但仍然没有工作,因为错误已经在clean方法中引发,所以我覆盖了它,而不是我的模型,它看起来像这样:

Class MyModel(models.Model):
    email = models.EmailField(max_length=70,blank=True)
    first = models.CharField(max_length=25,blank=True)
    last = models.CharField(max_length=25,blank=True)
    phase_id = models.CharField('The Phase', max_length=255, null=True, blank=True, unique=True)
    ...
    def clean(self):
        """
        Clean up blank fields to null
        """
        if self.phase_id == "":
            self.phase_id = None

这对我来说很有效,使用save的答案可能在某些情况下有效,这里应该通过在基类清理中进行其余验证之前将"重置为None来工作。欢呼:)

允许CharField为空并默认为None。只要没有多个空白字段(""),就不会引发完整性错误。

#models.py
Class Contact(models.Model):
    email = models.EmailField(max_length=70, blank=True, null=True, unique=True, default=None)
# protect the db from saving any blank fields (from admin or your app form)
def save(self, *args, **kwargs):
    if self.email is not None and self.email.strip() == "":
        self.email = None
    models.Model.save(self, *args, **kwargs)

最新更新