I don't know if work with many to many is the best approach, because if you
know that only have two emails for user and you check his email a lot then
the cost of join in time and machine resources increase innecessarily  So
knowledge of you requirements determine your db scheme.

Using the first solution I thing in something like this.
in models.py

class Person(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email1 = models.EmailField()
    email2 = models.EmailField(null=True, blank=True)



in your forms forms.py

from django.forms import ModelForm
from myapp.models import Person

class PersonForm(ModelForm):

    def clean(self):
        cleaned_data = super(PersonForm, self).clean()
        if cleaned_data['email1'] == cleaned_data['email2']:
            raise forms.ValidationError("Emails are equals ")

        ....


    class Meta:
        model = Person
        fields = ['first_name', 'last_name', 'email1', 'email2']


And if you want to use in admin site

class PersonAdmin(admin.ModelAdmin)
    form = PersonForm


2015-04-08 9:01 GMT-06:00 Bruno A. <alla.bru...@gmail.com>:

> +1 for Javier's answer, use a simple Foreign key on the e-mail field, not
> a ManyToMany on the Person (M2M allows an email to belong to multiple
> users). The Foreign key ensures an e-mail belongs to only 1 user, and that
> 2 users cannot have the same e-mail, but lets a user have multiple.
>
> To force all users to have at least one e-mail, something like this would
> do the job:
>
> class Person(models.Model):
>     first_name = models.CharField(max_length=100)
>     last_name = models.CharField(max_length=100)
>
>     def save(self, *args, **kwargs):
>         if self.emails.count() == 0:
>             raise ValueError("Need at least one email.")
>         return super(Person, self).save(*args, **kwargs)
>
>
> class Email(models.Model):
>     person = models.ForeignKey(Person, related_name='emails')
>     email = models.EmailField(unique=True)
>
>
>
> On Wednesday, 8 April 2015 04:02:50 UTC+1, victor menezes wrote:
>>
>> Would it be a bad approach to use a ManyToMany with properties null/blank
>> False?
>>
>> class Email(models.Model):
>>     email = models.EmailField(unique=True)
>>     def __unicode__(self):
>>         return self.email
>>
>> class Person(models.Model):
>>     first_name = models.CharField(max_length=100)
>>     last_name = models.CharField(max_length=100)
>>     emails = models.ManyToManyField(Email, null=False, blank=False)
>>     def __unicode__(self):
>>         return self.last_name + ', ' + self.first_name
>>
>>
>> On Tuesday, April 7, 2015 at 6:48:22 PM UTC-4, victor menezes wrote:
>>>
>>> Thanks for the help, I end up doing it was easy to implement the email
>>> as TabularInline inside the Person form in the Admin but how would you make
>>> mandatory to have at least one e-mail?
>>> Should I validate inside the save() method of Person class?
>>>
>>>
>>> On Tuesday, April 7, 2015 at 3:55:26 PM UTC-4, Javier Guerra wrote:
>>>>
>>>> On Tue, Apr 7, 2015 at 2:20 PM, victor menezes <menezes...@gmail.com>
>>>> wrote:
>>>> > 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.
>>>>
>>>>
>>>> it's a very bad idea to have plurality by adding a number of similar
>>>> fields.  instead, you should have a related table
>>>>
>>>> class Person(models.Model):
>>>>     first_name = models.CharField(max_length=100)
>>>>     last_name = models.CharField(max_length=100)
>>>>
>>>> class Email(models.Model):
>>>>     person = models.ForeignKey(Person)
>>>>     email = models.EmailField(unique=True)
>>>>
>>>>
>>>> --
>>>> Javier
>>>>
>>>  --
> 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/fe0288b9-9382-4163-b2cd-9d998f2e72f3%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/fe0288b9-9382-4163-b2cd-9d998f2e72f3%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
"La utopía sirve para caminar" Fernando Birri

-- 
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/CAG%2B5VyPpdfi9BNf16VRZ75-KXyU%3DOeBLsvzJqDKPR616dE_pVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to