繁体   English   中英

Django模型:电子邮件字段唯一,如果不为null /空白

[英]Django model: Email field unique if not 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的某些部分出于唯一性的目的将“”视为不应重复的内容。

解决方法是覆盖save方法,如下所示:

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的答案可能适用于某些情况,这里的这个应该通过在基类clean中进行其余验证之前将“”重置为None来工作。 干杯:)

允许CharField为null并将其默认为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)

暂无
暂无

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

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