假设你有一个简单的模型:
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)
有更好的方法吗?
答案 0 :(得分:20)
不幸的是,它并不像设置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)
答案 1 :(得分:10)
这样做:
class Contact(models.Model):
email = models.EmailField(max_length=70, null=True, blank=True, unique=True)
答案 2 :(得分:8)
我尝试使用保存,但仍然无法正常工作,因为已经在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
这对我很有用,使用保存的答案可能适用于某些情况,这个应该通过重置""在其余的验证发生在基类clean之前,为None。干杯:)
答案 3 :(得分:1)
允许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)