tags:

views:

33

answers:

2

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?

+1  A: 

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

Jannis
I'm going down this route. However, this is more me wondering if the EmailField can do it, or if I am doing something wrong.
The EmailField is basically a CharField but only validates against plain email adresses--no names or angle brackets. So, no, you're not doing anything wrong.
Jannis
+1  A: 

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

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))
Alasdair