Hi there,

What would be the best way to make a model with two e-mail fields uniques?
I was thinking about writing some validation in the save() method but would
like to know whether Django has some built-in way to deal with it.

class Person(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email1 = models.EmailField(null=True, blank=True)
    email2 = models.EmailField(null=True, blank=True)
    def __unicode__(self):
        return self.last_name + ', ' + self.first_name
    def save(self):
        if (len(self.email1) == 0 and len(self.email2) == 0):
            raise ValueError('At least one e-mail is needed to proceed.')
        super(Member, self).save()


John = Person()
John.email1 = 'someEmail'
John.save()

Kane = Person()
Kane.email1 = 'anotherEmail
Kane.email2 = 'someEmail' #same email used by John on email1 field
Kane.save() #should raise error because email2 is already used by John on
email1 field

Thanks


Victor Menezes
"Geek by nature, Linux by choice"

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAAgFNMmFNGYNEQwdGxvW9x81S1RNb-Wm_LYuk3cZ1YD4rb7RLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to