Any interest in a multi email field in django?

521 views
Skip to first unread message

JJ Zolper

unread,
Dec 1, 2014, 7:58:35 PM12/1/14
to django...@googlegroups.com
Hey everyone,

I'm just curious if anyone around the community has an interest in discussing the possibility of adding a "MultiEmailField" to Django?

Here is what I found when roaming the internet:


Basically, the reason I care is because on my website I'm building the ability to manage multiple e-mails on the user's profile. So on sites of the caliber of facebook, google, twitter, and so on allow a person to have multiple e-mails listed. Then for example select which one they want to be their "primary" e-mail. My thoughts would be then which ever one my user selects I would copy that selection from the "multiple email field management view" to the django.auth.user "email" field. So basically whatever they pick on my management for multiple emails set that to the default email field on a Django user. That way for my code that uses the django auth user email field for login handling (with the password) it can verify if it shall allow that user to login. Again, this primary email is the center of the entire users interaction in terms of authentication and I am aiming to show them say a check mark next to which one they have chosen. The rest of the e-mail addresses could serve other purposes. For sites like facebook, google, etc they could be recovery email addresses, but for me I would check the extensions. So for what I'm doing I would check within the list of emails the user has if say it has the "vt.edu" extension if they were trying to join a college community and so on.

So I think by now I've explained my reasoning behind wanting some sort of multiple email field. I would use it to set which email is the primary as well as allow them to add multiple emails to then verify their identities but also use some for recovery of an account for example.

Does anyone agree with me that they would like to see this functionality native in Django?

Thanks for your time,

JJ Zolper

Lachlan Musicman

unread,
Dec 1, 2014, 8:05:41 PM12/1/14
to django...@googlegroups.com
You don't really need it native in Django because the User Model is
easily extensible - giving you or others the opportunity to extend it
as you see fit and to release it the world as fle has.

See here:

https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#extending-the-existing-user-model


cheers
L.
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/c206b3ed-c434-456d-b978-b7fe67c8d0bd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
You have to be really clever to come up with a genuinely dangerous
thought. I am disheartened that people can be clever enough to do that
and not clever enough to do the obvious thing and KEEP THEIR IDIOT
MOUTHS SHUT about it, because it is much more important to sound
intelligent when talking to your friends.
This post was STUPID.
-----------------------------------------------------------------------------------------------------------
The Most Terrifying Thought Experiment of All Time
http://www.slate.com/articles/technology/bitwise/2014/07/roko_s_basilisk_the_most_terrifying_thought_experiment_of_all_time.html

Carl Meyer

unread,
Dec 1, 2014, 8:06:44 PM12/1/14
to django...@googlegroups.com
Hi JJ,
I agree with the use-case, but I don't think a MultiEmailField is the
best implementation. It is better to have a linked table of email
addresses with a ForeignKey to User (or perhaps an ArrayField, if you're
using Postgres, which will soon be built-in.)

Carl

signature.asc

JJ Zolper

unread,
Dec 1, 2014, 9:37:09 PM12/1/14
to django...@googlegroups.com
Lachlan,

Thanks for your input. I apologize if from my OP that it wasn't clear but I am already creating a Profile or "extending the user model". 

# Athlete User

class Athlete(models.Model):

    athleteuser = models.OneToOneField(User)

    athleteavatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True, default='images/default/no-img.jpg')

    athletebirthday = models.DateField(blank=True, null=True)

    athleteurl = models.CharField(max_length=30, unique=True, blank=True)              # Must limit to a-z && A-Z && and 0-9 chars, validators=[validate_slug]

    athletelanguage = models.CharField(max_length=15, blank=True)

    athletecommunities = models.ManyToManyField('communities.Community', blank=True, null=True)

    athletecolleges = models.ManyToManyField('colleges.College', blank=True, null=True)

    athletetwitterscreenname = models.CharField(max_length=30, blank=True)

    isVerified = models.BooleanField(default=False)

   

    User.profile = property(lambda u: Athlete.objects.get_or_create(athleteuser=u)[0])


Certainly because Django is so flexible a multi email field doesn't have to be native but I was more just getting a feel for any interest that others might have in having this capability available. To me it is a very standard use case, and again to me off the top of my head my opinion is that something that solves this use case could be a standard feature that is shipped, that's really all this is about.

For the time being I am trying basically your route and am doing things as an extension of what already exists.

Thanks a lot,

JJ

JJ Zolper

unread,
Dec 1, 2014, 9:47:00 PM12/1/14
to django...@googlegroups.com
Carl,

I went ahead and tried your implementation suggestion and I am at a point now that I am not sure what to do next:

# Athlete Email

class AthleteEmail(models.Model):

    athlete = models.ForeignKey(Athlete)

    email = models.EmailField(unique=True)

    verified = models.BooleanField(default=False)


This way basically any athlete which is the "profile" user extension wants to add an email they can in this way as this email object is FK'ed to their profile. Also, then once they verify an email the boolean there is flipped to true.

The next step to me at least is to then build a form from a modelform of the above and then render this in a view so the form:

class AthleteProfileEmailUpdateForm(ModelForm):

    athleteemail = forms.EmailField(label=(u'Athlete Email Address:'))


class Meta:

    model = AthleteEmail

    exclude = ('athlete', 'verified',)

    fields = ['email',]


At this point I'm not sure though, let me try to explain. For example on my profile module:

# Athlete User

class Athlete(models.Model):

    athleteuser = models.OneToOneField(User)

    athleteavatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True, default='images/default/no-img.jpg')

    athletebirthday = models.DateField(blank=True, null=True)

    athleteurl = models.CharField(max_length=30, unique=True, blank=True)              # Must limit to a-z && A-Z && and 0-9 chars, validators=[validate_slug]

    athletelanguage = models.CharField(max_length=15, blank=True)

    athletecommunities = models.ManyToManyField('communities.Community', blank=True, null=True)

    athletecolleges = models.ManyToManyField('colleges.College', blank=True, null=True)

    athletetwitterscreenname = models.CharField(max_length=30, blank=True)

    isVerified = models.BooleanField(default=False)

    

    User.profile = property(lambda u: Athlete.objects.get_or_create(athleteuser=u)[0])


You can see this line: User.profile = property(lambda u: Athlete.objects.get_or_create(athleteuser=u)[0])

then the form:

# Athlete Update Profile Form

class AthleteProfileUpdateForm(ModelForm):

    athleteavatar = forms.ImageField(label=(u'Athlete Avatar:'))

    athletebirthday = forms.CharField(label=(u'Athlete Birthday:'))

    athleteurl = forms.CharField(label=(u'Athlete Url:'))

    #athletecommunities = forms.MultipleChoiceField(label=(u'Athlete Communities:'), widget=forms.CheckboxSelectMultiple, required=False, choices=ATHLETECOMMUNITIESCHOICES)

    

    class Meta:

        model = Athlete

        exclude = ('athleteuser',)

        fields = ['athleteavatar', 'athletebirthday', 'athleteurl', 'athletecommunities']


which then allows me in a view to:

athleteprofile = AthleteProfileUpdateForm(instance = profile)

thereby displaying a form with the already submitted contents in the form. However, I'm not sure how to do this with my current situation. I would assume a similar approach so basically collecting all the email objects and more specifically the email fields within that object that relates to the current or logged in user profile and then for looping over those items in a form. To me that would be the way that then I could dynamically show them all emails they have and allow them to edit them. I believe if I could implement this I could figure out how to do the rest that I need.

Do you have any ideas?

Thanks so much!

JJ


Lachlan Musicman

unread,
Dec 1, 2014, 10:01:44 PM12/1/14
to django...@googlegroups.com
Hey JJ,

And I probably could have been less gruff.

What I was trying to say was, as someone that's been hanging in these
parts for a few years now, I can tell you that it is unlikely the devs
would introduce it to standard Django. That's why the BaseUser is so
base - it's specifically designed to be extenisble and easily
replaced, and that type of functionality - and you are very right here
- is probably needed by many, but not by every. And as such, is a
perfect candidate for an app or extension rather than inclusion in the
base. See Django Debug toolbar, or REST frame work or model-utils or
any number of other very useful apps - essential and invaluable for
some, not needed by all.

On another note, FYI there is an URLField that could be used on athleteurl

https://docs.djangoproject.com/en/1.7/ref/models/fields/#urlfield

or you could just create a slug per Athlete and then refer to each
Athlete by the get_absolute_url function? (I don't know if you've done
this and rejected it - but it's what I'd do straight up).

https://docs.djangoproject.com/en/1.7/ref/models/instances/#get-absolute-url


cheers
L.
> https://groups.google.com/d/msgid/django-users/e9001bd9-8823-41f2-921f-e9924d5e81f2%40googlegroups.com.

Carl Meyer

unread,
Dec 1, 2014, 10:50:59 PM12/1/14
to django...@googlegroups.com
Hi JJ,

To make a form for users to add/remove emails with a linked model
approach, you'll need an inline model formset:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

Carl
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@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/46257514-13d8-4a7e-ad34-2595d102fe7d%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/46257514-13d8-4a7e-ad34-2595d102fe7d%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

JJ Zolper

unread,
Dec 1, 2014, 11:02:46 PM12/1/14
to django...@googlegroups.com
Carl,

Thanks so much for your help, this suggestion will help a lot.

I actually wasn't really aware that this was even possible until now. Would explain why I wasn't sure if it could be done.

Thanks again,

JJ

JJ Zolper

unread,
Dec 1, 2014, 11:10:42 PM12/1/14
to django...@googlegroups.com
Lachlan,

Oh I absolutely understand. Don't get me wrong I prefer Django to be as lightweight as possible, and leave out things that aren't really that necessary. I'm with you. I have
also seen some ideas that I have turn into something people like and wish to pursue (rare but it has happened) so I just say what the heck sometimes when I think there
could be others that actually assemble and push for inclusion of a concept.

Also, agreed. I have mostly been doing gritty dev work and not optimizing as much so I understand how useful the django debug toolbar is now but again it isn't a part of
native django for sure.

Thanks for the tip on the urlfield, generally my implementations can be rough because django has a feature I am unaware of at the time of its inception or I just quickly jot
something up with the attention to go back and clean up (probably not the best approach but that's how i think. When I have an idea i just go and come back later and refine.
But you're totally right I should follow suit in terms of what django does on the athleteurl field.

Thanks for the continued input, I've enjoyed the discussion.

JJ

Lachlan Musicman

unread,
Dec 1, 2014, 11:30:48 PM12/1/14
to django...@googlegroups.com
All good - that's how I build too. The slug/get_absolute_url will blow
your mind. Makes everything easier.

L.
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/97c83eb1-fe68-42f0-896e-1a7f54a4190f%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages