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
support@acme.com
I want to store
"ACME Support" <support@acme.com>
The reason is, that when I send email, I would like a "friendly name" to appear. 开发者_JAVA百科Can this be done?
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))
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
精彩评论