I liked the idea at first but after couple tests realized that the it will 
alway raise the error on save().

The problem is that I can't add any Email without Person, this looks great 
but, I also can't add a Person without an Email and so it will never allow 
me to add anything.

Maybe I should keep it as ForeignKey but ensure that the Email is filled 
just in the form validation, not on database/class level? Following a test 
that i did:

>>> p = Person()

>>> p.first_name = 'aaa'
>>> p.last_name = 'bbb'
<Person: bbb, aaa>
>>> p.emails.all()
[]
>>> p.emails.count()
0
>>> p.emails.create(email='a...@aaa.aaa')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
...
(value, self.field.rel.to._meta.object_name)
ValueError: Cannot assign "<Person: bbb, aaa>": "Member" instance isn't 
saved in the database.
>>> p.save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  ...
    raise ValueError('Need at least one e-mail.')
ValueError: Need at least one e-mail.


On Wednesday, April 8, 2015 at 11:38:28 AM UTC-4, Bruno A. wrote:

> +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/2a9c7893-c2de-45de-ace8-a44207f6f460%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to