Krystian Cybulski
Krystian Cybulski

Reputation: 11108

Django EmailField and full email address with first and last name

I would like to use the EmailField in a form. However, instead of only storing

[email protected]

I want to store

"ACME Support" <[email protected]>

The reason is, that when I send email, I would like a "friendly name" to appear. Can this be done?

Upvotes: 4

Views: 8122

Answers (2)

Alasdair
Alasdair

Reputation: 308839

We use Django's email field, and then use a property to render the friendly name in the email.

from django.utils.html import escape
from django.utils.safestring import mark_safe

class MyModel(models.Model):
    email_address = models.EmailField()
    full_name = models.CharField(max_length=30)
    ...

    @property
    def friendly_email(self):
        return mark_safe(u"%s <%s>") % (escape(self.fullname), escape(self.email_address))

Upvotes: 3

jnns
jnns

Reputation: 5634

Why not store the friendly name in a separate CharField? Alternatively, you could subclass the EmailField and build your own validation. See http://docs.djangoproject.com/en/1.1/howto/custom-model-fields/#howto-custom-model-fields

Upvotes: 1

Related Questions